Copy Excel Chart to PowerPoint using VBA

KevinYC

New Member
Joined
Mar 27, 2011
Messages
12
Hello Everyone,

I am a new vba user. I am trying to copy an Excel chart into a powerpoint slide using vba. I got an error (Invalid porcedure call or argument) at this line of code "ActiveSheet.ChartObjects(chart_name).Activate" highlited in red. The code opens the powerpoint doc successfully but stops at the line colored in red. The chart's title in excel is 'income' and that's how I reference it in the fuction. What am I doing wrong?

Any input would be greatly appreciated!!

Here is my code:

Sub makePowerPoint()
...Dim PPT As PowerPoint.Application
Set PPT = New PowerPoint.Application
PPT.Visible = True
PPT.Presentations.Open Filename:="C:\My Documents\MacroTest.ppt"
Copy_chart "Sheet1", "income", 1, 250, 200, 60, 15
End Sub



Public Function copy_chart(sheet, chart_name, slide, awidth, aheight, atop, aleft)
Sheets(sheet).Select
' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPApp As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.slide
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
PPApp.ActiveWindow.View.GotoSlide (slide)
' Reference active slide
Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex)
ActiveSheet.ChartObjects(chart_name).Activate
ActiveChart.ChartArea.Copy
PPSlide.Select
PPSlide.Shapes.PasteSpecial ppPastePNG
PPSlide.Select
PPSlide.Shapes(PPSlide.Shapes.Count).Select
Dim sr As PowerPoint.ShapeRange
Set sr = PPApp.ActiveWindow.Selection.ShapeRange
' Resize:
sr.Width = awidth
sr.Height = aheight
If sr.Width > 700 Then
sr.Width = 700
End If
If sr.Height > 420 Then
sr.Height = 420
End If
' Realign:
sr.Align msoAlignCenters, True
sr.Align msoAlignMiddles, True
sr.Top = atop
If aleft <> 0 Then
sr.Left = aleft
End If
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Function
 
Last edited:
Comment out the On Error GoTo ErrorHandling line, so the debugger will highlight the line that gives you the error. Reinstate this line only after the program works.
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Comment out the On Error GoTo ErrorHandling line, so the debugger will highlight the line that gives you the error. Reinstate this line only after the program works.

I Commented 'On Error GoTo ErrorHandling' and post running the Macro now I'm at below error:

Error: 91 Object variable or With block variable not set

in below line

Code:
PP.ActivePresentation.Slides.Add PP.ActivePresentation.Slides.Count + 1, 11
 
Last edited:
Upvote 0
Okay, I don't know what you did with the module-level variables, but now you have variables declared in CopytoPPT that were assigned to objects in ExporttoPPT. Since CopyToPPT no longer knows what PP refers to, it chokes.

I've been meaning to do this for several posts, because it's making it more difficult for you to handle and for me to follow. Let's put the code together.

Code:
Option Explicit

Sub ExporttoPPT()

    Dim PP As PowerPoint.Application
    Dim PPPres As PowerPoint.Presentation
    Dim PPSlide As PowerPoint.slide
    Dim NextShape As Long
    Dim myRangeName As String
    Dim ActFileName As Variant

    ''On Error GoTo ErrorHandling '' disable for now

    ActFileName = Application.GetOpenFilename("Microsoft PowerPoint-Files (*.pptx), *.pptx")
    Set PP = CreateObject("Powerpoint.Application")
    If ActFileName = False Then
        PP.Activate
        PP.Presentations.Add
        Set PPPres = PP.ActivePresentation
    Else
        PP.Activate
        Set PPPres = PP.Presentations.Open(ActFileName)
    End If
    PP.Visible = True

    myRangeName = "myDashboard01"

    Application.GoTo Reference:=myRangeName
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Range("B1").Select
    PP.ActivePresentation.Slides.Add PP.ActivePresentation.Slides.Count + 1, 11
    Set PPSlide = PPPres.Slides(PP.ActivePresentation.Slides.Count)
    PPSlide.Shapes.Placeholders(ppPlaceholderTitle).TextFrame.TextRange.Font.Size = 36
    NextShape = PPSlide.Shapes.Count + 1
    PPSlide.Shapes.PasteSpecial 2
    PPSlide.Shapes(NextShape).Height = Application.InchesToPoints(5.56)
    PPSlide.Shapes(NextShape).Width = Application.InchesToPoints(11.9)

    Set PPSlide = Nothing
    Set PPPres = Nothing
    Set PP = Nothing
    Worksheets(1).Activate
    Exit Sub

ErrorHandling:

    Set PPSlide = Nothing
    Set PPPres = Nothing
    Set PP = Nothing
    MsgBox "Error No.: " & Err.Number & vbNewLine & vbNewLine & "Description: " & Err.Description, vbCritical, "Error"

End Sub
 
Upvote 0
Okay, I don't know what you did with the module-level variables, but now you have variables declared in CopytoPPT that were assigned to objects in ExporttoPPT. Since CopyToPPT no longer knows what PP refers to, it chokes.

I've been meaning to do this for several posts, because it's making it more difficult for you to handle and for me to follow. Let's put the code together.

Hi Jon, sorry for the trouble. But I really appreciate your effort and support on this. After putting down my last post I want back to my original code and made some tweaks and changes and it's working great. My only concern as if now is how to get the Title and Object's size right. I also referred to your website but still it seems I can't get that right.

Is there a way we fix this or does the user has to reshape it manually.

This might be the last fix and we would be good to go.

Thanks again for all your efforts.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

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