using Excel vba to create a powerpoint with 4 charts in each slide

mpalk

New Member
Joined
Jul 12, 2013
Messages
6
I am trying to write a macro that goes in an excel file that creates a powerpoint presentation, and puts four (4) charts in each slide.. I currently have a code that is pasting all of the charts in the same slide and I can't figure out why it isn't working (side note: I haven't attempted to resize or relocate the pictures on the powerpoint slides yet).. My code is as seen below and any help is greatly appreciated.

Code:
'Add a reference to the Microsoft PowerPoint Library by:
    '1. Go to Tools in the VBA menu
    '2. Click on Reference
    '3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay
 
 'keep button in same location
 Set btn = ActiveSheet.Shapes("CommandButton17")
With btn
 btLeft = .Left
    btTop = .Top
    End With
    
    'First we declare the variables we will be using
        Dim newPowerPoint As PowerPoint.Application
        Dim activeSlide As PowerPoint.Slide
        Dim cht As Excel.ChartObject
     
     'Look for existing instance
        On Error Resume Next
        Set newPowerPoint = GetObject(, "PowerPoint.Application")
        On Error GoTo 0
     
    'Let's create a new PowerPoint
        If newPowerPoint Is Nothing Then
            Set newPowerPoint = New PowerPoint.Application
        End If
    'Make a presentation in PowerPoint
        If newPowerPoint.Presentations.Count = 0 Then
            newPowerPoint.Presentations.Add
        End If
     
    'Show the PowerPoint
        newPowerPoint.Visible = True

        
    'Loop through each chart in the Excel worksheet and paste them into the PowerPoint
        For i = 1 To ActiveSheet.ChartObjects.Count
            Set cht = ActiveSheet.ChartObjects(i)
                    
'            With ActivePresentation.SlideMaster
'                .CustomLayouts.Add (1)
'                .CustomLayouts(1).Name = "Title And Content"
'            End With
        
        'Add a new slide where we will paste the chart
        If i - 1 Mod 4 = 0 Then
            newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutTitle
        End If
           
           
           newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
            Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
                
        'Copy the chart and paste it into the PowerPoint as a Metafile Picture
            cht.Select
            ActiveChart.ChartArea.Copy
            activeSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
    
        'Set the title of the slide the same as the title of the chart
            'activeSlide.Shapes(1).TextFrame.TextRange.Text = cht.Chart.ChartTitle.Text
            
        'Adjust the positioning of the Chart on Powerpoint Slide
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 165
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 150
            newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = 400
        
            activeSlide.Shapes(2).Width = 200
            activeSlide.Shapes(2).Left = 505
            
            activeSlide.Shapes(1).Top = 25
                        
        Next
     
    AppActivate ("Microsoft PowerPoint")
    Set activeSlide = Nothing
    Set newPowerPoint = Nothing
 
I don't know if this of any use but I know to select a certain page in a power point slide you can use this. So maybe you can add that before you transfer over the charts so they are on different slides.

Code:
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">SlideShowWindows(1).View.GotoSlide GetSlideIndex("Slide2"), 1
</code></pre>
 
Upvote 0
I can see how that could possibly be useful.. but I am having trouble fitting it into a loop or finding a way to use that method in a loop to make it work.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top