简体   繁体   中英

VBA save Powerpoint

I have managed to get my powerpoint template to open, then add in charts from excel, now I am trying to save it with a new name, however my code keeps falling over. The error is

Run-time error '91': Object variable or With block variable not set

Master code

Option Explicit

Public PPT As PowerPoint.Application
Public PPT_pres As PowerPoint.Presentation

Sub master()

Dim fileNameString As String

fileNameString = "C:\Users\Person\Desktop\Testfolder\Test1.pptx"

Call Module4.OpenPowerpoint

Call Module4.PasteCharts

PPT_pres.SaveAs fileNameString, 1 'ERROR HERE

PPT.Quit

End Sub

where Module4.OpenPowerPoint is

Public Sub OpenPowerpoint()
    Set PPT = New PowerPoint.Application

    PPT.Visible = True
    Set PPT_pres = PPT.Presentations.Open(Filename:="C:\Users\Person\Desktop\Testfolder\Test - Template.pptx")

End Sub

Past chart code - this repeats a few times for different graphs

Public Sub CopyPasteForecastTopGraph()
    If PPT Is Nothing Then Exit Sub
    If PPT_pres Is Nothing Then Exit Sub

    Dim rng As Range
    Dim mySlide As Object
    Dim myShape As Object
    Dim cht As Chart

    Set mySlide = PPT_pres.Slides(5)

    With mySlide
    .Select
    Set cht = ThisWorkbook.Worksheets("Forecast").ChartObjects("FcChart").Chart

       cht.CopyPicture Appearance:=xlScreen, Format:=xlPicture, Size:=xlScreen
       .Shapes.Paste.Select

        '''''''''''''''''''''''''''''''''
        'Paste as Chart and break link. '
        '''''''''''''''''''''''''''''''''
        'cht.ChartArea.Copy
        '.Shapes.Paste.Select


    'With .Shapes("HcChart")
        '.LinkFormat.BreakLink
    'End With

        PPT_pres.Windows(1).Selection.ShapeRange.Left = 35
        PPT_pres.Windows(1).Selection.ShapeRange.Top = 110
        PPT_pres.Windows(1).Selection.ShapeRange.Width = 720
        PPT_pres.Windows(1).Selection.ShapeRange.Height = 300

        End With

    'Clear The Clipboard
    Application.CutCopyMode = False
    Application.Wait (Now + TimeValue("00:00:01"))

End Sub

As you do not show the code of PasteCharts it's difficult to tell. Because you use global variables for your objects coding a PPT.quit is possible "messy", especially as you do not close PPT_pres and create always a new instance of Powerpoint in Module4.

Either you get rid of the global variable completely and re-factor your code or you could try to change the code in the following way after the line where the error occurs ppt_pres.SaveAs fileNameString, 1

ppt_pres.Close
Set ppt_pres = Nothing
PPT.Quit
Set PPT = Nothing

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