我正在尝试创建一个按钮,该按钮会将工作表“Graphs”中的图表导出为jpeg文件。这是我的代码,但是它继续显示此错误:
专门为此:
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
谢谢大家!
最佳答案
这是您要尝试的吗?
另外,如果您尝试将其另存为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