简体   繁体   中英

Export Excel charts as pictures to PowerPoint

I have in excel workbook with one chart per sheet and multiple sheets, I am trying to export all of the charts as pictures into PowerPoints The issue is that the current code I have doesn't work

Sub TEST_ExportChartsToPowerPoint_SingleWorkbook()
    'Declare PowerPoint Object Variables
    Dim pptApp As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation
    Dim pptSlide As PowerPoint.Slide
    Dim SldIndex As Integer
    'Declare Excel Object Variables
    Dim Chrt As ChartObject
    'Dim Chrt As ChartArea
    Dim WrkSht As Worksheet
    
    'Create a new instance of PowerPoint
    Set pptApp = New PowerPoint.Application
        pptApp.Visible = True

    'Create a new Presentation within the PowerPoint Application
    Set pptPres = pptApp.Presentations.Add

    'Create an index handler for the slide creation
    SldIndex = 1
    
    'Loop through all of the worksheets in the active work book
    For Each WrkSht In Worksheets
        'WrkSht.Select

        ActiveChart.ChartArea.Select
        ActiveChart.ChartArea.Copy
                       
        'Create a new slide, set the layout to blank, and paste the chart on the slide
        Set pptSlide = pptPres.Slides.Add(SldIndex, ppLayoutBlank)
        pptSlide.Shapes.Paste
                
        'Increment our slide index
        SldIndex = SldIndex + 1
    
    Next WrkSht
              
End Sub

One issue is that you have a With statement set up, but you never make use of it. The ActiveChart is a property of the Workbook object, which is the variable WrkSht in your code. Change these lines to have a . in front of them.

Change these two lines

ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Copy

to

.ActiveChart.ChartArea.Select
.ActiveChart.ChartArea.Copy

in your With block.

There is no guarantee that this is the (only) issue with your code since you did not tell us what error is occurring, or how your code is not giving you the expected results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM