Solved

How to run VBA Code when a PowerPoint Presentation (ppt) opens?

Posted on 2007-03-25
4
4,769 Views
Last Modified: 2012-08-13
How can I automatically run some VBA Code when I open up a PowerPoint 2003 presentation (ppt)?
0
Comment
Question by:BobSue
  • 2
4 Comments
 
LVL 7

Expert Comment

by:hippohood
ID: 18791287
As I understand you wnat to use events AfterPresentationOpen or PresentationOpen, which happens after an existing presentation file is open.
Noi, you can't, sorry, not in a simple way like in Excel or Winword. To have the events working you have to initialize the Application object BEFORE the file is open. This kind of chicken-egg problem. What you can is to run the Initialize event from a previously open file or manually, or create and install an add-in with an Auto_open macro.
That means you need the user to prepare his Powerpoint to run your macro by installing and Add-in. Or, somehow, run an initialization macro (only once) beforehand each time when he opens Powerpoint. If this is ok for you, I (or someone else) can teach you how to do that.
Another option is that you open your file using a shortcut, in which you can put a command to run some script.
0
 

Author Comment

by:BobSue
ID: 18794736
Thanks hippohood!
I like the command line option with the add-in option second.
Any assistance in either of those areas would be great.

Have a great day!
Bob
0
 
LVL 21

Expert Comment

by:GlennaShaw
ID: 18807349
As an alternative, you can use VB to initialize PowerPoint itself and totally control the whole presentation with VB.NET.

See: http://support.microsoft.com/kb/303717/en-us

Have a good one,
Glenna
www.pptmagic.com
0
 
LVL 7

Accepted Solution

by:
hippohood earned 500 total points
ID: 18815041
Sorry for delay. I was sure that someone can help you with add-ins.
Here is a code to create an add-ins.
1) Put the code in a new Module in VBA editor (Alt+F11).
2) compile it
3) save presentation  as PPA file
This add-in should be loaded on a machine where you want to use it.
4) From powerpoint Tools -> Add-Ins, "Add New", find and open that PPA file
To run the initialization macro press the new button on Toolbar. After that you can open your file with embedded event handler AfterPresentationOpen (see the MS help about that)

'===============
Dim X As New EventClassModule
Sub InitializeApp()
    Set X.App = Application
End Sub

Sub Auto_Open()
    Dim aToolbar As CommandBar
    Dim aButton As CommandBarButton

    Dim NewToolbarName$

    NewToolbarName  = "Initialize"

    Set aToolbar = CommandBars.Add(Name:=NewToolbarName , _
        Position:=msoBarFloating, Temporary:=True)
    If Err.Number <> 0 Then  
          ' The toolbar's already there, so we have nothing to do
          Exit Sub
    End If
    Set aButton = aToolbar.Controls.Add(Type:=msoControlButton)

    With aButton
         .DescriptionText = " Initialize "  
         .Caption = " Initialize!"    
         .OnAction = "InitializeApp"  
         .FaceId = 52      
    End With
End Sub
'==================================
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Background   Certain code in VBA requires initialization, such as application events. The app initialization is often triggered by the Auto_Open sub which is a special procedure that runs when an add-in loads. More significantly, this sub does n…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This video teaches viewers how to add simple and professional themes to their slides.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now