Supercharge your PowerPoint productivity with
Supercharge your PPT Productivity with PPTools - Click here to learn more.

PPTools

Image Export converts PowerPoint slides to high-quality images.

PPT2HTML exports HTML even from PowerPoint 2010 and 2013, gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements

Merge Excel data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more

Resize your presentations quickly and without distortion

Language Selector switches the text in your presentation from one language to another

FixLinks prevents broken links when you distribute PowerPoint presentations

Shape Styles brings styles to PowerPoint. Apply complex formatting with a single click.

Prep4PDF preserves interactivity in PowerPoint presentations when you convert to PDF

Show me the ID of PowerPoint's command bar controls (and launch them)

Problem

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?

Solution

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.


Did this solve your problem? If so, please consider supporting the PPT FAQ with a small PayPal donation.
Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2015 Stephen Rindsberg, Rindsberg Photography, Inc. and members of the MS PowerPoint MVP team. You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.
Español    Deutsch    Français    Português    Italiano    Nederlands    Greek    Japanese    Korean    Chinese

Supercharge your PPT Productivity with PPTools

content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

Show me the ID of PowerPoint's command bar controls (and launch them)
http://www.pptfaq.com/FAQ00946_Show_me_the_ID_of_PowerPoint-s_command_bar_controls_-and_launch_them-.htm
Last update 07 June, 2011
Created: