Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powerpoint VBA: Create class module to access events?

Status
Not open for further replies.

VBAjedi

Programmer
Dec 12, 2002
1,197
KH
Hi everyone. . . long time, no post.

I'm making a Powerpoint 97 add-in to do some of the repetitive dirtywork I go through every time I export my lecture notes from Word to Powerpoint. I have the core code done, but I need to create the custom menu item that loads when the add-in loads so I can fire off my code (since you can't assign keyboard shortcuts to macros in Powerpoint).

According to Combo in thread707-635078 ,
You can use Application events, see "Using Events with the Application Object" in PP VBA help file. As in other office applications, you have to create new class to handle events and initialise it. After that, SlideShowBegin and other events can be trapped.

I haven't used classes before (!), and I can't find that help file article (it's either not installed, or it was added in a later version of Powerpoint). Can someone give me some simple guidelines for coding a class that lets me access Presentation.Open and Presentation_BeforeClose events, or whatever Powerpoint calls them?

Thanks!

VBAjedi

For what it's worth, this is the code I'm trying to fire (as I would do it in Excel):
Code:
Private Sub Workbook_Open()
Dim MenuObject As CommandBarPopup
Dim MenuItem As CommandBarButton

For Each MenuObject In Application.CommandBars(1).Controls
   If MenuObject.Caption = "C&ustom Tools" Then MenuObject.Delete
Next MenuObject

Set MenuObject = Application.CommandBars(1). _
   Controls.Add(Type:=msoControlPopup, Temporary:=True) ' Before:=PositionOrMacro,
With MenuObject
   .Caption = "C&ustom Tools"
   Set MenuItem = .Controls.Add(Type:=msoControlButton)
   With MenuItem
      .Caption = "&Animate All"
      .OnAction = "AnimateAll"
   End With
   Set MenuItem = .Controls.Add(Type:=msoControlButton)
   With MenuItem
      .Caption = "&About Custom Tools"
      .OnAction = "ShowAbout"
      .BeginGroup = True ' Adds divider line
   End With
End With
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim MenuObject As CommandBarPopup
For Each MenuObject In Application.CommandBars(1).Controls
   If MenuObject.Caption = "C&ustom Tools" Then MenuObject.Delete
Next MenuObject
End Sub

VBAjedi [swords]
 
Create a Class module named, say, EventClassModule with this code:
Public WithEvents App As Application

Select App in the class module objects list and choose the events of interest in the Procedure list, e.g.:
Private Sub App_NewPresentation()
' your code here
End Sub

In a standard code module you have to instantiate your object:
Dim X As New EventClassModule
Sub InitializeApp()
Set X.App = Application
End Sub

Once the InitializeApp is executed your class module's App object is pointing to the actual Microsoft PowerPoint Application object and the event procedures you've written are triggered when the corresponding events raise.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top