Excel to PowerPoint Code Error

CT Witter

MrExcel MVP
Joined
Jul 7, 2002
Messages
1,212
Grabbed this code from this board but keep getting a code error. I have a referrence to MS PowerPoint 10.

Code:
Option Explicit

Sub MoveActiveChartToPPT()
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Dim strMsg As String
    
    If TypeName(ActiveSheet) <> "Chart" Then
        strMsg = "The wrong type of sheet is active."
        MsgBox strMsg, vbExclamation
        Exit Sub
    End If

    Dim appPPT As New PowerPoint.Application
    Dim pptActive As PowerPoint.Presentation
    Dim slideNew As Slide
    Dim shpChart As PowerPoint.Shape, shpTitle As PowerPoint.Shape
    Dim i%
    
    On Error Resume Next
    ' Reference existing instance of PowerPoint
    Set appPPT = GetObject(, "Powerpoint.Application")
    If Err.Number <> 0 Then
        ' PowerPoint not running, create new instance
        Set appPPT = CreateObject("Powerpoint.Application")
    End If
    On Error GoTo 0
   
   
    Set pptActive = appPPT.ActivePresentation
    
    
    Application.ScreenUpdating = False
    Application.CutCopyMode = False
    ActiveChart.ChartArea.Copy
    With pptActive
        Set slideNew = .Slides.Add(Index:=.Slides.Count + 1, _
                                   Layout:=ppLayoutTitleOnly)
    
        slideNew.Shapes.PasteSpecial ppPasteEnhancedMetafile
    End With
    
    Set shpChart = slideNew.Shapes(slideNew.Shapes.Count)
    With shpChart
        .Left = pptActive.PageSetup.SlideWidth * 0.05
        .Top = 100
        .Width = pptActive.PageSetup.SlideWidth * 0.9
'        .ScaleWidth 1.22, msoFalse, msoScaleFromTopLeft
'       .ScaleHeight 1.22, msoFalse, msoScaleFromTopLeft
    End With

    Set shpChart = Nothing
    Set slideNew = Nothing
    Set pptActive = Nothing
    Set appPPT = Nothing
    Application.ScreenUpdating = True
    Application.CutCopyMode = False

End Sub

Works fine if PPT is open, errors on the if to create a new presenation. Error is "Application (unknown member): Invalid request. There is no active presentation.

What do I need to do differently?

Thanks,
CT
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,021
CT,

I believe I was the author of the code you've cited. (Although the calls to GetObject are not mine.)

In order to create a new presentation these would be the key lines:
Code:
...
Dim pptNew As PowerPoint.Presentation
...
Set pptNew = appPPT.Presentations.Add
...
Set slideNew = pptNew.Slides.Add(Index:=pptNew.Slides.Count + 1, _
                                   Layout:=ppLayoutTitle)

<hr>

Oaktree,

The use of the generic "Object" variable type along with the "CreateObject" method is more flexible, but it also precludes compile-time error checking and using named constants. Personally, I go for creating the reference and the early bind myself. [Most of the time] "getting it to work" has nothing to do with whether you used the generic Object or the specific object type. The code will either work or it won't (most of the exceptions to this rule that I have come across have been related to control types on user forms where you can do things with a "Control" object that you can't do with a specific control type such as a "combobox" or "listbox" so you use the "generic" and make things work that should work for the combobox or listbox types but won't).

HTH
 

Forum statistics

Threads
1,148,108
Messages
5,744,874
Members
423,907
Latest member
zerocool88

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
Top