Opening Existing PowerPoint file via Excel VBA

John Davis 1 Reputation point
2021-09-19T06:50:38.507+00:00

Hi
I want to open an existing PowerPoint file (nominated by the user) using VBA within Excel. Note that I have added the PowerPoint Object library to 'References' in the macro. The code I am using (see below) I have used before successfully to open Excel files. In this instance, however, I have replaced "Workbooks.Open" with "Presentations.Open". The code successfully allows the user to browse for their file, but when the file is selected, the macro crashes at "Presentations.Open". Can someone pls tell me what I am missing? Thank you in anticipation of your help

'OPEN USER FILE
Dim my_Filename As Variant
Hello = "Browse to find your presentation. . ."
MsgBox Hello, , "POWERPOINT UPDATER"
my_Filename = Application.GetOpenFilename(filefilter:="PowerPoint Files,.ppt;.pptx")
If my_Filename <> False Then
Presentations.Open Filename:=my_Filename
End If

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tom van Stiphout 1,621 Reputation points MVP
    2021-09-19T14:35:57.637+00:00

    I'm taking you literally that you only want to open a file and not subsequently manipulate it.
    Then use 1 line of code, with the built-in Shell function - see help file.

    1 person found this answer helpful.
    0 comments No comments

  2. John Davis 1 Reputation point
    2021-09-20T02:00:08.263+00:00

    Thank you both very much for taking the time to respond - I really appreciate.

    Actually I need to do a great deal more than just open the PowerPoint file - but I was just trying to keep my query focused in the first instance. FYI after opening the file, I want to sequentially skip through the slides in the file, and on each slide
    (i) identify charts
    (ii) for each chart, open the datasheet and make various changes to the datasheet
    (iii) then move the location of some shapes to re-align their location relative to the bars (or whatever) in the chart

    In response to John Korchok (Hi John)....the GetOpenFilename bit works fine - it allows the user to browse for their file and select it - which allows me to identify the file name and assign the name to the variable 'my_Filename'. The problem is actually opening the file.

    I CAN, however, open 'my_Filename' if I use this approach:

    Sub Test_6()
    'OPEN USER FILE
    Hello = "Browse to find your presentation. . ."
    MsgBox Hello, , "POWERPOINT UPDATER"
    my_Filename = Application.GetOpenFilename(filefilter:="PowerPoint Files,.ppt;.pptx")
    Dim MyPPT As Object
    Set MyPPT = CreateObject("PowerPoint.Application")
    MyPPT.Visible = True
    MyPPT.Presentations.Open my_Filename

    ' I then want to sequentially skip through the slides in the file, and on each slide (i) identify charts (ii) for each chart
    ' open the datasheet and make various changes to the datasheet (iii) then move the location of some shapes to re-align
    ' their location relative to the bars (or whatever) in the chart

    End Sub

    But having opened the file this way, I ground to a halt in trying to even do something simple like, in the first instance, identify how many slides there are in the file by using 'ActivePresentation.Slides.Count'

    So I started doing more searching and came across the concepts of 'early binding' and 'late binding' - which was a bit of revelation for a novice like me!! Based on my understanding of these concepts, if I give the Excel macro access to the PowerPoint object library, I could use 'early binding' and the macro could use all the bits in the PowerPoint library without having to things like Dim the PowerPoint file as an Object and set it using Create Object.

    So logically then, it seemed that I could use GetOpenFilename to identify the name of the file, and then simply use Presentations.Open Filename:=my_Filename to open it.

    So given all that, I am still at a loss as what the missing piece is. (Declaration: if it's not already obvious, my (limited) experience with VBA to date has been writing routines for Excel. This is my first foray into attempting to use VBA with PowerPoint).

    0 comments No comments