1

so I've found that PowerPoint 2007 does indeed support event handlers on the Application level. For example Application.NewPresentation or even Application.AfterNewPresentation

It's described here http://msdn.microsoft.com/en-us/library/ff745073.aspx

But the real question is, how do I use these? Where do I use them? To tell you my scenario, what I want is to insert dynamic text into a few textboxes. I have the macro code to do this and it's working exactly as I want it to. But I need the macro to fire once a new presentation is created from a potm template. And only on that event. Just like it does in Word 2007.

Where do I start? I tried to just create a sub looking like this and saving it as a potm file and open a new presentation based on that template. And nothing happened.

Private Sub App_NewPresentation(ByVal Pres As Presentation)
    MsgBox "Running!"
End Sub

Edit: It is possible to open any Office 2007 file with an XML editor. I use the Custom UI Editor For Microsoft Office and in that I add a Office 2007 Custom UI.XML part following the guide presented here: http://www.pptalchemy.co.uk/PowerPoint_Auto_Open_Code.html

But I run into problems when PowerPoint creates a new presentation based on that template. Opening the template in itself works just fine. The event handler is there and the code is run beautifully. But a new presentation based on it? No way, the handler is there as well. But it says it cannot find the macro. Even though the macro is in the new presentation as well since I can open Visual Basic Editor and find the macro and then run it. It's just the autopart that doesn't seem to be working like it should.

Todd Main
  • 28,951
  • 11
  • 82
  • 146
Kenny Bones
  • 5,017
  • 36
  • 111
  • 174

2 Answers2

2

The only way to create an auto macro in PowerPoint VBA is to have your file as an Add-in (.ppa or .ppam - not .pptx/.pptm/.potm/etc). And the way to create it is:

  1. Create a class module. At the top (after any Option XXX), put Public WithEvents App As Application and then put your routine above below that.
  2. Create a module of any name and put:

    Dim X As New Class1
    Sub AutoOpen()
        Set X.App = Application
        ''# Code to create new presentation
    End Sub
    

Again, this will not work from your requirement of .potm. Another way that you can consider has been depreciated, but it still works, which is to create a Wizard file.

Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • Are you sure? I know this was the case in PowerPoint 2003. But in 2007 you have the option of editing the XML data of the file itself. That's how I initially created an autostart handler in a potm file. Anyway, if I were to create an add-in, the add-in would include a class module with Public WithEvents App As Application. And the powerpoint template actually being used is what should include the code block you provide? Am I getting this correctly? – Kenny Bones Sep 17 '10 at 05:47
  • I don't get it. I created an add-in file which contains the Public WithEvents App As Application. But I cannot reach "App" from anywhere. I mean, I start a new presentation, open Visual Basic Editor and create a new Class Module. In this I put Public WithEvents App As Application on the top. Then I create a new module, but from the same presentation, I cannot reach "App" at all. When typing Dim X as New App, "App" isn't even in the list. This is the same when I try to Dim X as New App from any other presentation as well. I says "Object required" – Kenny Bones Sep 17 '10 at 06:02
  • Apologies for the mis-coding. The name of your class you created is what you want to use. So if you just created it and there are no other classes, it is probably "Class1". Then just use that. `Dim X As New Class1`. Ive changed the code above to reflect that. – Todd Main Dec 21 '10 at 23:48
  • Can you tell how to create a class module? If I create a Module and paste in "Public WithEvents App As Application", I'd get an error. Is there many kinds of Modules in PowerPoint VBA? – Pompair Jul 26 '12 at 16:14
  • @Pompair: Go to the "Insert" menu and then choose "Class Module" – Todd Main Jul 26 '12 at 16:56
0

The macro in the presentation that was created has no way to run, as the auto_open macro only works with addins. Based on the way you're doing it, you'd have to reload the ribbon to kick the event in your new presentation you want to run.

Meihua
  • 241
  • 1
  • 9
  • 22
  • I found that I can create an Add-In and put code in that. But at the point that the code from the add-in is run, the code from the template can't run. I mean, I can reach the code and see if it exists, but I can't run it. The solution so far is to run all of the code from the add-in itself. And just do a check to see if the template name equals something I want. To make sure the code is only run in the templates I want and not globally. – Kenny Bones Sep 20 '10 at 06:16
  • You mean even if you use `Application.Run` you can't run the code? Like here: http://www.pptfaq.com/FAQ00403.htm – Meihua Sep 21 '10 at 22:27