从 Excel 2012 导出一组图形对象(图表和其他线条)
我有 VBA 代码,可以从 Excel 以 PNG 格式导出活动图表。
我有一些点和线,标记了一些覆盖在我的 Excel 图表上的重要数据,并且它们被分组(选择所有对象和图表,右键单击 -> 分组)。
有什么可以替换 ActiveChart 的东西(如 ActiveGroup 或类似的)来导出整个内容,而不仅仅是图表。
Sub ExportChartToPNG()
'Take ActiveChart and copy it as a GIF image to the same directory as the Workbook is in and name it with the Chart_Title with spaces replaced with underscores.
Dim chtCopyChart As Chart, sCurrentDirectory As String, sFileName As String
Dim x As Integer, CellCharacter As String
Dim sInteractive As Boolean
Set chtCopyChart = ActiveChart
sCurrentDirectory = ActiveWorkbook.Path
sFileName = chtCopyChart.ChartTitle.Text
sFileName = InputBox("Enter filename for export:", "Export object", sFileName)
For x = 1 To Len(sFileName)
CellCharacter = Mid(sFileName, x, 1)
If CellCharacter Like "[</*\?%]" Then
sFileName = Replace(sFileName, CellCharacter, "_", 1) ', Replaces all illegal filename characters with "_"
End If
If Asc(CellCharacter) <= 32 Then
sFileName = Replace(sFileName, CellCharacter, "_", 1) ' Replaces all non printable characters with "_"
End If
Next
sFileName = sFileName & ".png"
sFileName = sCurrentDirectory & "\" & sFileName
sInteractive = True
chtCopyChart.Export Filename:=sFileName, FilterName:="PNG", Interactive:=sInteractive
MsgBox "Chart copied to " & sFileName, vbOKOnly, "Success!"
End Sub
I have VBA code that exports the active chart from Excel in PNG format.
I have some dots and lines, marking some important data overlaid on my Excel chart, and they are grouped (select all objects and chart, Right Click -> Group).
Is there anything that I can replace the ActiveChart with (like ActiveGroup or similar) to export the whole thing, not just the chart.
Sub ExportChartToPNG()
'Take ActiveChart and copy it as a GIF image to the same directory as the Workbook is in and name it with the Chart_Title with spaces replaced with underscores.
Dim chtCopyChart As Chart, sCurrentDirectory As String, sFileName As String
Dim x As Integer, CellCharacter As String
Dim sInteractive As Boolean
Set chtCopyChart = ActiveChart
sCurrentDirectory = ActiveWorkbook.Path
sFileName = chtCopyChart.ChartTitle.Text
sFileName = InputBox("Enter filename for export:", "Export object", sFileName)
For x = 1 To Len(sFileName)
CellCharacter = Mid(sFileName, x, 1)
If CellCharacter Like "[</*\?%]" Then
sFileName = Replace(sFileName, CellCharacter, "_", 1) ', Replaces all illegal filename characters with "_"
End If
If Asc(CellCharacter) <= 32 Then
sFileName = Replace(sFileName, CellCharacter, "_", 1) ' Replaces all non printable characters with "_"
End If
Next
sFileName = sFileName & ".png"
sFileName = sCurrentDirectory & "\" & sFileName
sInteractive = True
chtCopyChart.Export Filename:=sFileName, FilterName:="PNG", Interactive:=sInteractive
MsgBox "Chart copied to " & sFileName, vbOKOnly, "Success!"
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这个老问题,但解决方案来自这样一个事实:与其他形状分组的图表成为工作表中的形状对象。因此,您实际上需要做的是获取对形状对象的引用,该形状对象是您创建的组。
但是,形状没有导出方法,因此您需要创建一个临时空白图表,将形状复制到其中,导出新图表,然后将其删除。
步骤如下:
获取形状对象并将其复制为图片
创建一个与源形状尺寸相同的新图表对象
将对象从剪贴板粘贴到新图表
导出图表,如果需要,删除现有文件
然后删除图表并清理物体。
Old question I know, but the solution comes from the fact that a chart grouped with other shapes becomes a shape object in the worksheet. So what you actually need to do is get a reference to the shape object which is the group you've created.
However, there's no export method on shapes, so you need to create a temporary blank chart, copy the shape into it, export the new chart, then delete it.
The steps are:
Get the shape object and copy it as a picture
Create a new chartobject with the same dimensions as the source shape
Paste the object from the clipboard to the new chart
Export the chart, deleting an existing file if needed
Then delete the chart and clean up objects.
下面的代码用于保存一组形状的图像。这是对杰里米答案的修改,它找到一个特定的组(基于“格式形状”下找到的[替代文本]标题)。子程序首先运行特定的宏(以更新组中的图形)。
Here is code that works to save an image of a group of shapes. It's a modification of Jeremy's answer, that finds a specific group (based on the [Alt Text] Title found under 'Format Shape'). The sub runs a specific macro first (to update the graph in the Group).