VBA宏将图表导出到图像

时间:2016-10-08 07:38:45

标签: vba excel-vba excel

我在使用VBA脚本将图表导出到图像时遇到错误。我正在做的是导出为图像并将其作为报告发送到电子邮件中。但是,有些图表可以正常导出,而其他图表只显示为空文件。

Sub Emailer()

Application.Calculation = xlCalculationManual
Application.Calculate
DoEvents
Application.Calculate

Application.Calculate
    Dim rng As Range
    Dim rng2 As Range

    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next

    Sheets("Reporting Sheet").ChartObjects("Chart 3").Chart.Export "C:\Users\JudaL\Desktop\Reporting\1.png"
    Sheets("Reporting Sheet").ChartObjects("Chart 5").Chart.Export "C:\Users\JudaL\Desktop\Reporting\2.png"
    Sheets("Reporting Sheet").ChartObjects("Chart 9").Chart.Export "C:\Users\JudaL\Desktop\Reporting\3.png"
    Sheets("Reporting Sheet").ChartObjects("Chart 7").Chart.Export "C:\Users\JudaL\Desktop\Reporting\4.png"
    Sheets("Reporting Sheet").ChartObjects("Chart 10").Chart.Export "C:\Users\JudaL\Desktop\Reporting\5.png"
    Sheets("Reporting Sheet").ChartObjects("Chart 11").Chart.Export "C:\Users\JudaL\Desktop\Reporting\6.png"

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next

    With OutMail
        .To = "Test"
        .CC = ""
        .BCC = ""
        .Subject = "Test"
        .HTMLBody = "<img src='C:\Users\JudaL\Desktop\Reporting\1.png' />" & "<img src='C:\Users\JudaL\Desktop\Reporting\2.png' />" & "<img src='C:\Users\JudaL\Desktop\Reporting\3.png' />" & "<img src='C:\Users\JudaL\Desktop\Reporting\4.png' />" & "<img src='C:\Users\JudaL\Desktop\Reporting\5.png' />" & "<img src='C:\Users\JudaL\Desktop\Reporting\6.png' />"



        .Display   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing

Application.Calculation = xlCalculationAutomatic

End Sub

0 个答案:

没有答案