Author: smeans
Go to Source
I’ve been working on a series of videos about an unpublished app of mine called FlipStream, and I wanted to easily insert some information about the current PowerPoint presentation on the first slide. Sadly, PowerPoint doesn’t have a generic field replacement function like Word does. So, either I type some stuff and have to remember to change it every time I make a new presentation, or I write my own field replacement logic. For the true hacker, there is no contest: time to dust off the old VBA reference.
My goal is to add some handlebar-style references to the text in my slides and then look up the associated document properties using VBA. In the PowerPoint Presentation
object there are a couple of properties that we can take advantage of to populate our fields: BuiltInDocumentProperties
and CustomDocumentProperties
.
For reference, the
property recognizes the following property names:BuiltInDocumentProperties
Title
Subject
Author
Keywords
Comments
Template
Last author
Revision number
Application name
Last print date
Creation date
Last save time
Total editing time
Number of pages
Number of words
Number of characters
Security
Category
Format
Manager
Company
Number of bytes
Number of lines
Number of paragraphs
Number of slides
Number of notes
Number of hidden Slides
Number of multimedia clips
Hyperlink base
Number of characters (with spaces)
Content type
Content status
Language
Document version
It also adds a custom property that gives the time that the presentation was started:
Presentation start time
So, given those lists, I could create a slide that looks like this:
And when I start the presentation, it would look like this:
So, to do this substitution, we need to be notified whenever a presentation is starting or ending. We can accomplish this by writing event handlers for the PowerPoint Application object. In this case, we want to handle the SlideShowBegin
and SlideShowEnd
events. The pseudocode for this process looks like:
for each slide: for each shape: if shape contains text and text contains a field reference {{: preserve original text in Tags replace text with contents of properties
Then, when the presentation is over, the cleanup process is:
for each slide: for each shape: if shape has original text in Tags: restore original text delete Tag
This was all pretty straightforward, and I was able to get it working quickly. But in the end, the most challenging part of this process was actually registering my event handlers when my presentation opens! I’m guessing it’s partially a byproduct of security concerns and partially inertia, but it isn’t possible to have any VBA code execute when a PowerPoint presentation is loaded. So, after a lot of searching, I decided to put my tag code into a PowerPoint Add-in.
The documentation for developing a PowerPoint Add-in is pretty spotty, but the key thing is that when an Add-in is loaded, PowerPoint calls the following two subroutines:
Sub Auto_Open() ' initialization code goes here End Sub Sub Auto_Close() ' cleanup code goes here End Sub
The only other (minor) challenge is the logistics of developing and debugging an Add-in. I’m delivering my Add-in as a .ppam
file, but once you’ve saved your Add-in as a .ppam
file, you can no longer edit it directly. So I did all of my development in a .pptm
(PowerPoint Macro-Enabled Presentation) file and then used Save As to save as a .ppam
file for testing. You will probably need to use the File/Options/Add-ins dialog to remove your plug-in before you can overwrite the file for another testing round. That function is somewhat hidden in the Manage: PowerPoint Add-ins drop-down.
I’ve made the code available under the MIT license on GitHub. Enjoy!