简体   繁体   中英

Saving a PowerPoint in Excel VBA

I have code that creates a new powerpoint consisting of some images from an excel file. I want to save the file using a string variable to define its name. I've done my due diligence searching for solutions with no success, which surprises me based on how basic of a task I'm trying to complete. As of now, I have...

newPowerPoint.ActivePresentations.SaveAs filenamestring, 1
newPowerPoint.ActivePresentations.Close

But I keep getting a whole host of error messages. I have newPowerPoint defined as public in another module

Public newPowerPoint As powerpoint.Application

Any suggestions?

I think you are dimensioning the variable oPPTApp without actually creating an instance of Powerpoint.Application.

Public ppApp As PowerPoint.Application

Sub PPTFile()

Dim ppPres As Presentation
Dim fileNameString As String

fileNameString = "C:\testPPT.pptx" '<change to your file path/name

'Create an instance of PPT to work with
Set ppApp = CreateObject("Powerpoint.Application")
ppApp.Visible = True

'Create a new presentation (or you can access an existing file with ppApp.Presentations.Open
Set ppPres = ppApp.Presentations.Add

'Save:
ppPres.SaveAs fileNameString, 1

'Quit the instance of PPT that you initiated above.
ppApp.Quit

End Sub

EDIT

As you're adding slides using the AddSlide method, you need to refer to a CustomLayout .

Dim sldCount As Integer

sldCount = ppPres.Slides.count
ppPres.Slides.AddSlide sldCount + 1, ppPres.Slides(sldCount).CustomLayout
'Once you've added the slide, then set using Layout:
ppPres.Slides(sldCount + 1).Layout = ppLayoutBlank

Alternatively, you can use the old .Add method which accepts the Layout argument, instead of .AddSlide (which requires a CustomLayout):

ppPres.Slides.Add sldCount + 1, ppLayoutBlank

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