11

我正在尝试创建一个按钮,它将“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

谢谢大家!

4

3 回答 3

31

这是你正在尝试的吗?

另外,如果您尝试将其保存为 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
于 2012-08-13T17:25:19.250 回答
6

谢谢,我需要这个来提取图像中的所有图表,现在快午夜了。对上面的代码进行微小的更改就可以了。

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
于 2016-11-29T15:42:48.653 回答
0

根据@Miles 和@Siddarth Rout 给出的示例,我刚刚创建了这个变体。需要注意的是,这是针对跟踪用户反馈的应用程序的后端,需要以不同的方式针对各种受众进行细分。当我们等待开发人员构建应用程序的报告端时,我正在以 .csv 格式从 SQL 下载数据,然后在 Excel 中处理其中的废话,然后提供27个单独的图表和图形(即将成为 28如果我们能让最终的用户组上线!

我浏览并命名了每个图表,我需要调用结果文件,这样我就可以将图形上传到应用服务器,覆盖前一周的图表,自动(对于最终用户)更新报告。

在 Excel 中重命名图形对象

由于文件名需要保持完全相同,命名图表最有意义,所以我再也不需要触及那部分了。(直到今天这样做,我一直在复制每个图表,粘贴到 Paint,然后另存为 PNG,并保存前一周的文件,这样我就可以点击旧的,它会自动将该名称分配给另存为过程。找到上面的代码和一种使它更流畅的方法将每周为我节省大约 30 分钟并消除 27 个可能的手动错误......相当不错的投资!如果我能做到......一年前?

' Ctrl-Shift-A to run

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

        ' Change from above: using ChartObject.Name to individually name each exported PNG file
        myFileName = SaveToDirectory & objChrt.Name & ".png" 
        
        On Error Resume Next
        Kill SaveToDirectory & WS.Name & Index & ".png"
        On Error GoTo 0

        myChart.Export Filename:=myFileName, Filtername:="PNG"
    Next
Next

MsgBox "Eeeaaagle!!"
End Sub
于 2021-10-18T15:06:10.157 回答