简体   繁体   中英

Open a PowerPoint presentation from Excel with VBA and then set that presentation to a variable

I have to post a lot of Excel charts to a specific PowerPoint document and I'm building out a macro in Excel VBA to do it for me.

I'm able to correctly open the PowerPoint presentation that I want to update, however I don't know how to set the presentation I just opened to a variable called MyPresentation .

Dim myPresentation As PowerPoint.Presentation
Dim PowerPointApp As PowerPoint.Application

PowerPointApp.Presentations.Open Filename:="obscured filepath and name"`

Obviously there's some additional code, but I'm trying to set the Presentation I just opened in line 3 set to the MyPresentation variable so I can reference the document I just opened.

First you have to pave the way for using ppt files, what I did:

Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation

Set PowerPointApp = CreateObject("PowerPoint.Application")

DestinationPPT = "path"
PowerPointApp.Presentations.Open (DestinationPPT)

I ended up finding a solution by the MVP Andy Pope.

Some relevant code snippets for future users. (FYI My PPT was already visible when I ran into the problem)

Dim DestinationPPT As String
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation

'Easier to define manually set links up front so it's easier to change/modify
DestinationPPT = "C:\yourfilepath\yourfilename.pptx"`

Lookup the Spreadsheet Guru's guide to opening PPT from Excel VBA

Then:

Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)

I was trying to create an excel file that can generate a predefined powerpoint presentation on the basis of a specific worksheet, with different values within the excel-file.

My issue was that every person in my department should be able to download the excel-file and run it.

Defining Presentations within vba doesn't work for everyone since the reference that is required for this action isn't activated on every computer/excel-programm.

Createobject did the trick for me to get around the problem. Here the code i am talking about:

Dim pptApp as object
Dim strpath as string

strpath = ThisWorkbook.Path & "\Test_Dummy.pptx"
Set pptApp =  createobject("Powerpoint.application")

pptApp.Presentations.Open Filename:=strpath, readonly:=false, untitled:=true, withwindow:=true

What didn't work was stuff like:

Dim pptApp as Powerpoint.Application

or

Dim pptApp as object
Set pptApp = New Powerpoint.Application

without setting the required references in vba

This one worked for me:

'devlare variables
Dim MyPPT As Object

'create PowerPoint
Set MyPPT = CreateObject("Powerpoint.application")
'make it visible
MyPPT.Visible = True
'path to powerpoint
MyPPT.presentations.Open "path\powerpoint.pptx"

Set visible to true, before open it. otherwise it did not worked for me

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