Sdílet prostřednictvím


Programming Office Commandbars - get the ID of a CommandBarControl

The FindControl(FindControls) method of CommandBars collection/CommandBar object is the most useful and reliable way to get the reference to a CommandBarControl object. For built-in menu items and toolbar buttons, the control ID is required for a successful call to FindControl(s). The following VBA code snippet will list control IDs of all available CommandBarControl objects(i.e., menu items and toolbar buttons) in Microsoft Word:

 'retrieve IDs of all CommandBarControl objects by enumerating every CommandBar
'object in the CommandBars collection
Sub DumpBars()
PrintLine "Command bar controls", 1
Dim bar As CommandBar
For Each bar In Application.CommandBars
DumpBar bar, 2
Next
End Sub

'retrieve IDs of all CommandBarControl objects in a CommandBar object
Sub DumpBar(bar As CommandBar, level As Integer)
PrintLine "CommandBar Name: " & bar.Name, level
Dim ctl As CommandBarControl
For Each ctl In bar.Controls
DumpControl ctl, level + 1
Next
End Sub

'retrieve the ID of a CommandBarControl object. If it is a CommandBarPopup object,
'which could serve as a control container, try to enumerate all sub controls.
Sub DumpControl(ctl As CommandBarControl, level As Integer)
PrintLine vbTab & "Control Caption: " & ctl.Caption & vbTab & "ID: " & ctl.ID, level
Select Case ctl.Type
'the following control type could contain sub controls
Case msoControlPopup, msoControlGraphicPopup, msoControlButtonPopup, msoControlSplitButtonPopup, msoControlSplitButtonMRUPopup
Dim sctl As CommandBarControl, ctlPopup As CommandBarPopup
Set ctlPopup = ctl
For Each sctl In ctlPopup.Controls
DumpControl sctl, level + 1
Next
Case Else
'no sub controls
End Select
End Sub

'Word only output function. You should modify this if you are working with
'other Office products.
Sub PrintLine(line As String, level As Integer)
Selection.Paragraphs(1).OutlineLevel = level
Selection.InsertAfter Space(level * 4) & line & vbNewLine
Selection.Collapse wdCollapseEnd
End Sub

The above code works also with Excel, PowerPoint, FrontPage, etc., provided that appropriate changes are made to the "PrintLine" function for output. With minor modification (using an Explorer object instead of the application object in the "DumpBars" sub routine, e.g., Application.ActiveExplorer), it works for Microsoft Outlook as well. You can also port it to VBScript or JScript to find certain control IDs in InfoPath.

Comments

  • Anonymous
    September 30, 2004
    how can i get all the control ids for and subcontrols etc for powerpoint
  • Anonymous
    September 30, 2004
    Tony - all you need do is to change the PrintLine function and make it output results in a slide.
  • Anonymous
    January 15, 2006
    Can you elaborate a little more on using this code with Outlook. I've tried:

    Sub DumpBars()
    PrintLine "Command bar controls", 1
    Dim bar As CommandBar
    For Each bar In Application.CommandBars
    DumpBar bar, 2
    Next
    End Sub

    but I get an object required error.

    I put the code in an Outlook session.

    Thanks
  • Anonymous
    May 08, 2006
    The comment has been removed
  • Anonymous
    June 13, 2009
    PingBack from http://barstoolsite.info/story.php?id=22