我正在尝试创建一个按钮,将图表“Graphs”中的图表导出为jpeg文件。这是我的代码,但它继续显示此错误:
运行时错误424:需要对象
特别为此:
Set myChart = Graphs.ChartObjects(3).Name = "Chart4"
这是代码
Sub ExportChart()
Dim myChart As Chart
Dim myFileName As String
Set myChart = Graphs.ChartObjects(3).Name = "Chart4"
myFileName = "myChart.jpg"
On Error Resume Next
Kill ThisWorkbook.Path & "\" & myFileName
myChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"
MsgBox "OK"
Set myChart = Nothing
End Sub
谢谢大家!
答案 0 :(得分:24)
这是你在尝试的吗?
此外,如果您尝试将其保存为jpg,那么为什么要使用png过滤器?我已将“myChart.jpg”更改为“myChart.png”。适用时更改。
Sub ExportChart()
Dim objChrt As ChartObject
Dim myChart As Chart
Set objChrt = Sheets("Graphs").ChartObjects(3)
Set myChart = objChrt.Chart
myFileName = "myChart.png"
On Error Resume Next
Kill ThisWorkbook.Path & "\" & myFileName
On Error GoTo 0
myChart.Export Filename:=ThisWorkbook.Path & "\" & myFileName, Filtername:="PNG"
MsgBox "OK"
End Sub
答案 1 :(得分:4)
谢谢,我需要这个来提取图像中的所有图表,而且几乎是午夜。 上面代码的微小改动就可以了。
Sub ExportChart()
Dim WS As Excel.Worksheet
Dim SaveToDirectory As String
Dim objChrt As ChartObject
Dim myChart As Chart
SaveToDirectory = ActiveWorkbook.Path & "\"
For Each WS In ActiveWorkbook.Worksheets
WS.Activate 'go there
For Each objChrt In WS.ChartObjects
objChrt.Activate
Set myChart = objChrt.Chart
myFileName = SaveToDirectory & WS.Name & "_" & objChrt.Index & ".png"
On Error Resume Next
Kill SaveToDirectory & WS.Name & Index & ".png"
On Error GoTo 0
myChart.Export Filename:=myFileName, Filtername:="PNG"
Next
Next
MsgBox "OK"
End Sub