VBA Code belongs to a Presentation
VBA Code cannot stand alone. It must be contained inside a Powerpoint Presentation,
or an Excel Spreadsheet or a Word Document or other Office application.
A Presentation is a .pptx file.*
* -or any type of Powerpoint extension like .pptm or .ppt or .potx .etc.
A presentation contains slides. A slide contains shapes.
Declare a Presentation object variable
We declare a variable named pres that can be assigned to a presentation object (PowerPoint file)
Next we SET the variable pres to a specific presentation object (.pptx).
Set the presentation variable to a NEW presentation object
* Add a new presentation (.pptx) to the Presentations collection
This will open a new .pptx file.
The variable pres will be assigned to this new PowerPoint file.
The new file will not have a name until it is saved.
It will be saved as a .pptx because the variable pres was declared as a PowerPoint file.
Set the presentation variable to an EXISTING presentation object and OPEN it.
* Open a new presentation (.pptx) to add to the Presentations collection
This will open an existing .pptx file and assign the presentation variable to the file.
Set the presentation variable to an already opened file.
All open PowerPoint files are part of the Presentations collection.
Use any of these commands to specify which open file you want to assign the variable name pres.
Assign (SET) the variable by NAME
(These two statements are identical)
The file extension is optional, since it will be .pptx.
Do not include the path. The file is already open.
Assign (SET) the variable by referencing the ActivePresenation
Set pres = ActivePresentation
This will set the presentation variable to the currently selected PowerPoint file.
NOTE: The ActivePresentation can be useful if you use only have one file open.
Otherwise be sure that the correct .pptx file is selected.
Assign (SET) the variable by referencing the Index number of the .pptx file.
(These two statements are identical)
This will set the presentation variable to the PowerPoint file with an index of 1.
All open PowerPoint files (.pptx) are assigned an index when they are opened.
The index will be a number from 1 to the total number of open PowerPoint files.NOTE: You cannot rely on knowing the index number of an open PowerPoint file.
Index numbers are assigned automatically as PowerPoint files are opened and closed.
It is best to reference a Presentation by its name.
Saving a presentation
pres.Save
pres.SaveAs "c:\NewPresentation.pptx"
If you use the "Save" option, the presentation will be saved under its existing name.
If the presentation was opened as a new presentation, a "SaveAs" dialog box will be displayed to ask for a file name.