Show me the ID of PowerPoint's command bar controls (and launch them)
Sometimes you want to open a dialog box or perform some other action for your users but PowerPoint doesn't reveal any built-in method for doing what you want. It's frustrating, especially since the method's obviously built-in ... the user simply has to click a button to make it happen.
Why can't your code do the same?
Quite often it can.
The CommandBars.FindControl(Id:=xxx).Execute method will often do what you're after. It executes (read: simulates a user mouseclick on) the control whose ID is xxx. All you need to do is work out what xxx is. So first, you do something like this to list all the command bars in PowerPoint. Press Ctrl+G to open the Immediate window if it's not already visible in the VBA IDE.
Sub ListAllControls() Dim oCmdBar As CommandBar Dim oCtrl As CommandBarControl For Each oCmdBar In Application.CommandBars Debug.Print oCmdBar.Name Next End Sub
Locate the command bar you want from the listin in the Immediate window, then use this to list all the controls on the chosen command bar. This example lists the controls on "Menu Bar", the main PowerPoint menu bar, names and IDs.
Sub ListMainMenuControls() Dim oCmdBar As CommandBar Dim oCtrl As CommandBarControl Dim oCtrl2 As CommandBarControl For Each oCmdBar In Application.CommandBars Debug.Print oCmdBar.Name ' Substitute the name of the command bar you want here. If oCmdBar.Name = "Menu Bar" Then For Each oCtrl In oCmdBar.Controls Debug.Print vbTab & oCtrl.Caption If oCtrl.Caption = "Sli&de Show" Then For Each oCtrl2 In oCtrl.Controls Debug.Print vbTab & vbTab & oCtrl2.Caption & vbTab & oCtrl2.Id Next Exit Sub End If Next End If Next End Sub
Note the ID of the control you want and use this to "launch" the control (ie, click it for the user, so to speak):
Sub FireAControl(lngID as Long, strErrorMessage as String) On Error Resume Next Call CommandBars.FindControl(Id:=lngID).Execute If Err.Number <> 0 Then MsgBox strErrorMessage Err.Clear End If End Sub Sub DemonstrateIt() FireAControl 2892, "Please select something first" End Sub
See How do I use VBA code in PowerPoint? to learn how to use this example code.