Consulting

Results 1 to 5 of 5

Thread: Running VB code on PPT startup

  1. #1
    VBAX Newbie
    Joined
    Nov 2009
    Location
    Southampton, England
    Posts
    2
    Location

    Question Running VB code on PPT startup

    Hi there,

    I have been tasked with creating a PowerPoint template that prompts the user for some text then inserts it onto the Slide Master. I have created a VBA form that works pretty well, however there is a big problem - executing the code in the first place!

    I want to make this as simple as possible for our users as most of them only have very basic PPT skills so it is definitely too much to ask for them to go into the Master and manually edit the text or to go to Tools > Macros etc. All I want to do is to run the code automatically as soon as the user opens the PPT file.

    In Word, Excel or Access, this is easy to do by using an AutoExec macro or an event, however I have had no success in handling events in PPT. I looked at the VBA Help, which is not particularly helpful but does suggest doing this is possible. I then consulted Google and it came up with a website that said how to do this, however my attempts at following the instructions below do not work:

    three Ws dot PPTFAQdotcom slash FAQ00004 dot HTM - sorry, can't post links

    I have downloaded the AutoEvents Add-In and it is definitely loaded - there is a checkbox next to it in the Add-Ins dialogue box.

    I then followed the instructions under 'How to create the handler', but they are a bit ambiguous. Steps 1 and 2 are obvious, step 3 instructs me to enter the line [vba]Public WithEvents PPTEvent As Application
    [/vba] into the new class module, I have done this and named the class 'cEventClass' as per the instructions.

    Step 4 says to select 'PPTEvent' in the left-hand dropdown box above the code window and then the events are listed in the right-hand combo box, this is the case so I have selected 'PresentationOpen'. It then says to put in the code you want to run so I have just put in a msgbox.

    Step 5 says to create a new module sheet but it doesn't say what to name the sub or the module itself, I don't think the module name matters here, but surely the sub does? After this, my understanding of the instructions muddies considerably. "Add the following line: [vba]Dim cPPTObject As New cEventClass[/vba]", I have done that and just named the sub 'Events1' and left the module name as the default for now.

    Still under step 5, it says
    This is done because we need to instantiate an object of the cEventClass and then set the reference of the object variable PPTEvent to the powerpoint application.. The reference is set in the TrapEvents routine by the line given below:
    Set cPPTObject.PPTEvent = Application
    In the EventHandler Demo, look up the routine TrapEvents for this statement. Execute it. This will activate the eventhandler
    . I'm afraid that has totally lost me. I have added the line [vba]Set cPPTObject.PPTEvent = Application[/vba] beneath the previous Dim line.

    Now to step 6, it says
    To terminate the eventhandler, set the refernce of the object variable to Nothing (just like any other object variable) like this:
    Set cPPTObject.PPTEvent = Nothing
    In the file this statement will be encountered in the ReleaseEvent routine.
    That makes no sense to me either, why does the event handler need to be terminated using code? Surely the whole idea is for it to run until the presentation containing the event handler has been closed? In any case, I added the line of code it suggested, again beneath the previous line.

    Finally, the page says
    NOTE: An Event handler cannot be set automatically. To set an event handler when PowerPoint starts up you still need to rely on the Auto_Open macro of an add-in to instantiate the event handler.
    I would assume the Add-In I downloaded has this Auto-Open macro?

    Right, sorry if I've lost you! If you know of a better way of doing this, I am all ears, as I am tearing my hair out trying to get this thing working, particularly when I have spent so much time carefully creating the user form and the code for handling it!

    To summarise, here is the contents of my VBA project:

    - VBAProject (test.ppt)
    - Modules
    Module1
    - Class Modules
    cEventClass

    Module1 contains the following code:

    [vba]Sub Events1()
    Dim cPPTObject As New cEventClass
    Set cPPTObject.PPTEvent = Application
    Set cPPTObject.PPTEvent = Nothing
    End Sub[/vba]

    cEventClass contains the following:

    [vba]
    Public WithEvents PPTEvent As Application

    Private Sub PPTEvent_PresentationOpen(ByVal Pres As Presentation)
    MsgBox "Opened"
    End Sub
    [/vba]

    I have tried to attach my test PPT file with the code in but it says 'invalid attachment'. There is also the Add-In, which may be causing the problem but I can't open PPA files to edit, it is just the one I downloaded from

    H two Ts and a P two forward slashes skp dot mvs dot org, forward slash autoevents dot HTM

    As I said previously, I would be extremely grateful to any assistance you can offer, I have spent the best part of a day and a half trying to do something that I could do in a couple of minutes in Excel!

    If there is a way of doing this that does not require the Add-In then that would be preferred, as having to distribute the Add-In along with the template just creates more work although if not then no problem.

    If doing it from PowerPoint is not feasible then is it possible to create an Excel file with an autoexec procedure that fires up PowerPoint, opens the presentation and displays the VBA form?

    Thanks in advance,

    Gary

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,719
    Location
    Q: what version of PP?

    Paul

  3. #3
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,095
    Location
    As Paul asks the version is VERY important
    In 2007 you can do this via XML
    In earlier versions you really need to write an add in or have a button on the first slide that runs the auto_open code.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,095
    Location
    Code in the standard module should be

    [vba]
    Dim cPPTObject As New cEventClass
    Sub Auto_Open
    Set cPPTObject.PPTEvent = Application
    End sub

    Sub Auto_Close
    Set cPPTObject.PPTEvent = Nothing
    End Sub
    [/vba]
    Note though Auto_Open and Close ONLY work properly in an Add In
    Last edited by John Wilson; 11-11-2009 at 07:41 AM.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Newbie
    Joined
    Nov 2009
    Location
    Southampton, England
    Posts
    2
    Location
    Thanks, I'll try that but I would assume the add-in I downloaded would have the required events in?

    The version by the way is PowerPoint 2003 (sorry, I should have said that in my original post!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •