Export Excel Charts as Images using Powerpoint VBA

感情迁移 提交于 2019-12-06 08:05:07

Never mind I figured it out:

Sub ChartsToPowerPoint()

    Dim pptApp As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation
    Dim pptSlide As PowerPoint.Slide

    'Open PowerPoint and create an invisible new presentation.
    Set pptApp = New PowerPoint.Application
    Set pptPres = pptApp.Presentations.Add(msoFalse)

    'Set the charts and copy them to a new ppt slide
    'I could have also used for every chart object line
    'but I have only 2 charts

    Set objChart = Worksheets("Sheet1").ChartObjects("Chart 1").Chart
    objChart.ChartArea.Copy
    Set pptSlide = pptPres.Slides.Add(1, ppLayoutBlank)
    pptSlide.Shapes.Paste

    Set objChart = Worksheets("Sheet1").ChartObjects("Chart 2").Chart
    objChart.ChartArea.Copy
    pptSlide.Shapes.Paste

    'Save Images as png
    path = "C:\Users\xyz\Desktop\"

    For j = 1 To pptSlide.Shapes.Count
        With pptSlide.Shapes(j)
        .Export path & j & ".png", ppShapeFormatPNG
        End With
    Next j

    pptApp.Quit

    Set pptSlide = Nothing
    Set pptPres = Nothing
    Set pptApp = Nothing

End Sub

I figured out how to improve the quality of Charts.Export output. The image's size is linked to the zoom of the chart's sheet.

Sub ExportChart()
    Application.ScreenUpdating = False
    ActiveWindow.Zoom = 275
    Dim path1 As String
    path1 = "C:\path\path\path\image.png"


    ActiveSheet.ChartObjects("chart name").Activate
    ActiveChart.Export FileName:=path1, FilterName:="PNG"
    ActiveWindow.Zoom = 47

    Application.ScreenUpdating = True
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!