VBA - How to Find and Edit the Office 2003 legacy CommandBars and menus in Office 2010 (add, remove, edit controls in 'Menu Commands' group from Add-Ins tab)

Today I am going to show you how to work with legacy CommandBar controls in Office 2010.

Let's suppose you are in the following scenario:
-  you have created a DOT template in Office 2003;
- a couple of macros have been added to the VB project;

- and you also found it convenient to insert a custom menu item to allow end-users / colleagues to easily execute your macros;
- to create this custom menu, the Office 2003 built-in features helped you arrange and organize the items (open Word 2003 > click on Tools > Customize > 'Commands' tab > .. scroll down to 'New Menu' entry> select it > from the right-hand side panel drag the menu using your mouse > release it onto the 'Main Menu' bar, next to the built-in 'Help' menu);

- next you went back to the 'Customize' window, scrolled to the 'Macros' group and dragged the entries found there inside your custom menu (you also created a sub-menu :-) to group two similar macros);

 

- before closing the file, you went to the 'Customize' window and selected the current document (not 'Normal.DOT') from the 'Save in:' drop down;

- once the document was saved, everything worked in Office 2003, until someone tried to open it in Word 2010;
- that person noticed that the menu is shown in the 'Add-ins' tab inside a group named 'Menu Commands';

- you want to be able to add new items inside this menu in Office 2010, but you also want the documents to be backwards compatible; So we cannot use the newer XML ribbon feature;

What can you do?

 > tried to check if the "Menu Commands" group appears inside the visual menu (Ribbon) editor of Word 2010, but there is no such thing here!
> you just found the group can be added to the Quick Access Toolbar, but its controls are locked and there is no way to access them from the user interface;

 

 

Try to run the following code:

> it lists all built-in and custom CommandBar entries found in the application's main menu bar (even though Word 2010 does not use a Menu Bar anymore because it replaced it with the Ribbon, it still exposes these controls to the VBA object model for compatibility reasons);
> we have to write the output to a TXT file, because the code returns too many lines and the Debug window won't display them all;

The script I have offered is just a proof of concept and should not be put into production without a thorough testing!

Microsoft is not responsible if your users will lose data because of this code. It’s your responsibility to test it before deployment in your organization.

Sub listcmd()Dim cmd As CommandBarDim fsoSystem As ObjectDim file As ObjectDim logPath As String

If (fsoSystem Is Nothing) Then Set fsoSystem = CreateObject("Scripting.FileSystemObject") logPath = ThisDocument.FullName & "_Log.txt" 'object.OpenTextFile(filename[, iomode[, create[, format]]]) 'The "iomode" argument can have any of the following settings:    ' > ForReading: 1 Open a file for reading only. You can't write to this file.    ' > ForWriting: 2 Open a file for writing.    ' > ForAppending: 8 Open a file and write to the end of the file. If fsoSystem.FileExists(logPath) Then Set file = fsoSystem.OpenTextFile(logPath, 8, False) Else Set file = fsoSystem.CreateTextFile(logPath, False, False) End IfEnd If

file.WriteLine "[" & Strings.Format(Now, "dd-MMM-yyyy HH:nn:ss") & "] " & txt

On Error Resume NextFor Each cmd In Application.CommandBars 'If cmd.BuiltIn = False Then Debug.Print "Command Bar: " & cmd.Name & " Visible [" & cmd.Visible & "]" file.WriteLine "Command Bar: " & cmd.Name & " Visible [" & cmd.Visible & "]" Dim ctrl As CommandBarControl For Each ctrl In cmd.Controls Debug.Print " " & ctrl.Caption & " >> " & ctrl.OnAction file.WriteLine " " & ctrl.Caption & " >> " & ctrl.OnAction Next 'End IfNext

file.CloseEnd Sub

Output ========================================================================== [30-Mar-2012 21:09:17] Command Bar: Standard Visible [True] New &Blank Document >> &Open... >> &Save >> Permission (Unrestricted Access) >> &Mail Recipient >> &Print >> Print Pre&view >> &Spelling and Grammar... >> &Research... >> Cu&t >> &Copy >> &Paste >> &Format Painter >> Hyperl&ink... >> &Tables and Borders Toolbar >> &Drawing >> &Document Map >> &Show All >> Microsoft Word &Help >> &Read >> Command Bar: Formatting Visible [True] &Styles... >> &Bold >> &Italic >> &Underline >> Align &Left >> &Center >> Align &Right >> &Justify >> &Distributed >> &Left-to-Right >> &Right-to-Left >> &Numbering >> &Bullets >> &Decrease Indent >> &Increase Indent >> Command Bar: Tables and Borders Visible [False] Dra&w Table >> &Eraser >> &Merge Cells >> S&plit Cells... >> Distribute Rows Eve&nly >> Distribute Columns Evenl&y >> &Change Text Direction >> Sort &Ascending >> Sort Des&cending >> &AutoSum >> Command Bar: Database Visible [False] &Data Form >> &Manage Fields >> &Add Record >> &Delete Record >> Sort &Ascending >> Sort Des&cending >> &Database... >> &Update Field >> &Find in Field >> &Main Document >> Command Bar: Drawing Visible [False] &Select Objects >> &Line >> &Arrow >> &Rectangle >> &Oval >> Te&xt Box >> Draw &Vertical Text Box >> &WordArt... >> Dia&gram... >> &Clip Art... >> &Picture... >> Ink &Drawing and Writing >> Command Bar: Forms Visible [False] &Edit Box >> C&heck Box >> &Combo Box >> Prope&rties >> Dra&w Table >> &Frame >> &Show Field Shading >> &Reset Form Fields >> &Protect Form >> Command Bar: Full Screen Visible [False] &Close Full Screen >> Command Bar: Edit Picture Visible [False] &Adjust Margins >> &Close Picture >> Command Bar: Visual Basic Visible [False] &Macros... >> &Record New Macro... >> &Security... >> &Visual Basic Editor >> Control T&oolbox >> &Design Mode >> Command Bar: Stop Recording Visible [False] Stop &Recording >> &Pause Recording >> Command Bar: Mail Merge Visible [False] &Main document setup >> &Open Data Source >> &Mail Merge Recipients >> &Insert Address Block >> &Insert Greeting Line >> &Insert Merge Fields >> &Show Fields/Values >> &Highlight Merge Fields >> &Match Fields >> &Propagate Labels >> &First >> &Previous >> &Next >> &Last >> &Find Entry >> &Error Check Merge >> &Merge to Document >> &Merge to Printer >> &Merge to E-mail >> &Merge to Fax >> Command Bar: Master Document Visible [False]Command Bar: Microsoft Visible [False] Microsoft &Excel >> Microsoft &PowerPoint >> &Microsoft Outlook >> Microsoft &Access >> Microsoft Visual &FoxPro >> Microsoft P&roject >> Microsoft &Schedule+ >> Microsoft Pu&blisher >> Command Bar: Header and Footer Visible [False] &Page Number >> &Number of Pages >> &Format Page Numbers... >> &Date >> &Time >> Page Set&up... >> &Hide Body Text >> &Link to Previous >> &Show Header/Footer >> &Previous Header >> &Next Header >> &Close >> Command Bar: Outlining Visible [False] Promote to &Heading 1 >> &Promote >> &Demote >> Demote to &Body Text >> Move &Up >> &Move Down >> &Expand >> &Collapse >> &First Line Only View >> &Show Formatting >> &Update TOC >> &Go to TOC >> &Master Document View >> &Collapse Subdocuments >> &Create Subdocument >> &Delete Subdocument >> &Import Subdocument >> &Merge Subdocument >> &Split Subdocument >> &Protect Subdocument >> Command Bar: Print Preview Visible [False] &Print >> &Magnifier >> &One Page >> Ru&ler >> &Shrink One Page >> F&ull Screen >> &Close >> Command Bar: Word for Windows 2.0 Visible [False] New &Blank Document >> &Open... >> &Save >> Cu&t >> &Copy >> &Paste >> &Numbering >> &Bullets >> &Decrease Indent >> &Increase Indent >> &Frame >> &Drawing >> C&hart >> &Envelope >> &Spelling and Grammar... >> &Print >> &One Page >> &Zoom 100% >> &Fit To Window >> Command Bar: Read Mail Visible [False] &Reply >> Reply to A&ll >> For&ward >> &Print >> &Copy >> &Move Mail >> &Delete Mail >> &Item >> &Item >> &AutoFormat... >> &Message Header >> &Document Map >> &Show All >> Microsoft Word &Help >> Command Bar: Send Mail Visible [False] M&ail Recipient (as Attachment)... >> Select &Names... >> Chec&k Names >> &Properties >> &Save >> &Print >> &Spelling and Grammar... >> Cu&t >> &Copy >> &Paste >> &Format Painter >> Hyperl&ink... >> Fi&le... >> &AutoFormat... >> &Message Header >> &Document Map >> &Show All >> Microsoft Word &Help >> Command Bar: Extended Formatting Visible [False] &Dot Accent >> &Double Strikethrough >> Phonetic G&uide... >> &Combine Characters... >> &Enclose Characters... >> &Single Spacing >> &1.5 Spacing >> &Double Spacing >> Command Bar: AutoText Visible [False] AutoTe&xt... >> &Create AutoText... >> Command Bar: Web Visible [False] &Back >> &Forward >> &Stop >> &Refresh >> &Start Page >> Search the &Web >> &Show Only Web Toolbar >> Command Bar: WordArt Visible [False] &WordArt... >> Edit Te&xt... >> &WordArt Gallery >> Format &Object... >> &WordArt Same Letter Heights >> &WordArt Vertical Text >> Command Bar: 3-D Settings Visible [False] &3-D On/Off >> &Tilt Down >> &Tilt Up >> &Tilt Left >> &Tilt Right >> Command Bar: Shadow Settings Visible [False] &Shadow On/Off >> &Nudge Shadow Up >> &Nudge Shadow Down >> &Nudge Shadow Left >> &Nudge Shadow Right >> Command Bar: Picture Visible [False] &Picture... >> &More Contrast >> &Less Contrast >> &More Brightness >> &Less Brightness >> &Crop >> Rotate &Left 90° >> &Compress Pictures... >> Format &Object... >> &Set Transparent Color >> &Reset Picture >> Command Bar: Drawing Canvas Visible [False] Fi&t >> Ex&pand >> S&cale Drawing >> Command Bar: Organization Chart Visible [False] &Autoformat >> Command Bar: Diagram Visible [False] I&nsert Shape >> Move Shape &Backward >> Move Shape &Forward >> &Reverse Diagram >> &AutoFormat >> Command Bar: Reviewing Visible [False] &Previous >> &Next >> Ink &Annotations >> &Ink Comment >> New Co&mment >> &Track Changes >> Reply with &Changes... >> E&nd Review... >> Reviewing Pane >> Command Bar: Exit Design Mode Visible [False] &Design Mode >> Command Bar: Control Toolbox Visible [False] &Design Mode >> Propert&ies >> &View Code >> C&heck Box >> &Text Box >> Command &Button >> &Option Button >> &List Box >> Combo Bo&x >> To&ggle Button >> Spi&n Button >> &Scroll Bar >> L&abel >> &Image >> Command Bar: Text Box Visible [False] C&reate Text Box Link >> &Break Forward Link >> &Previous Text Box >> &Next Text Box >> &Change Text Direction >> Command Bar: Outlook Read Mail Visible [False] &Reply >> Reply to A&ll >> For&ward >> &Print >> &Copy >> &AutoFormat... >> &Move Mail >> &Delete Mail >> &Item >> &Item >> &Document Map >> &Show All >> Microsoft Word &Help >> Command Bar: Outlook Send Mail Visible [False] M&ail Recipient (as Attachment)... >> &Save >> &Print >> Cu&t >> &Copy >> &Paste >> &Format Painter >> Select &Names... >> Fi&le... >> &Show All >> Microsoft Word &Help >> Command Bar: Function Key Display Visible [False] &Function Key 1 >> &Function Key 2 >> &Function Key 3 >> &Function Key 4 >> &Function Key 5 >> &Function Key 6 >> &Function Key 7 >> &Function Key 8 >> &Function Key 9 >> &Function Key 10 >> &Function Key 11 >> &Function Key 12 >> Command Bar: Web Tools Visible [False] Web &Design Mode >> Propert&ies >> &Checkbox >> &Option Button >> &Drop-Down Box >> &List Box >> &Textbox >> T&ext Area >> &Submit >> Submit with &Image >> &Reset >> &Hidden >> &Password >> &Movie >> &Sound >> Sc&rolling Text >> Command Bar: Word Count Visible [False] Re&count >> Command Bar: Japanese Greetings Visible [False] &Greeting... >> O&pening... >> Clo&sing... >> I&nsert Previous Greeting >> Command Bar: Menu Bar Visible [True] SampleMenu >> Command Bar: &Legacy Keyboard Support Visible [False]Command Bar: Refresh Visible [False] Refre&sh >> Do &Not Refresh >> Command Bar: Frames Visible [False] &Table of Contents in Frame >> New Frame &Left >> New Frame Ri&ght >> New Fra&me Above >> New Frame &Below >> &Delete Frame >> Frame &Properties... >> Command Bar: E-mail Visible [False] &Save >> &Print >> Cu&t >> &Copy >> &Paste >> &Bold >> &Italic >> &Underline >> Align &Left >> &Center >> Align &Right >> &Distributed >> &Left-to-Right >> &Right-to-Left >> &Numbering >> &Bullets >> &Decrease Indent >> &Increase Indent >> Tran&slate >> &Additional Actions >> Command Bar: Full Screen Reading Visible [False] &Save >> &Print >> &Document Map >> Thum&bnails >> Fi&nd... >> &Research... >> &Increase Text Size >> &Decrease Text Size >> Show Printed &Page >> &Allow Multiple Pages >> Allow T&yping >> Margin Settings >> Edit >> Command Bar: Document Layout Visible [False] Unlock Document Layout >> &Help >> Command Bar: Compare Side by Side Visible [False] &Synchronous Scrolling >> &Reset Window Position >> Compare Side &by Side with... >> Command Bar: Ink Drawing and Writing Visible [False] &Eraser >> &Select Objects >> &Stop Erasing >> Command Bar: Ink Annotations Visible [False] Stroke &Eraser >> &Select Objects >> M&arkup >> &Stop Inking >> Command Bar: Ink Comment Visible [False] &Pen >> &Eraser >> Command Bar: Visible [False]Command Bar: Drop Caps Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Borders and Shading... >> &Drop Cap... >> Command Bar: Endnotes Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Font... >> &Paragraph... >> &Style... >> &Go to Endnote >> &Note Options... >> Con&vert to Endnote >> B&lock Authors >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> &Additional Actions >> Command Bar: Fields Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Edit Citation >> Edi&t Source >> &Update Field >> &Edit Field... >> &Toggle Field Codes >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> Command Bar: Display Fields Visible [False] Cu&t >> &Copy >> &Paste >> Download &Pictures >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Edit Citation >> Edi&t Source >> &Edit Field... >> &Toggle Field Codes >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> Command Bar: Field Display List Numbers Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Edit Citation >> Edi&t Source >> &Edit Field... >> &Toggle Field Codes >> &Decrease Indent >> &Increase Indent >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> Command Bar: Form Fields Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Edit Citation >> Edi&t Source >> Prope&rties >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Command Bar: Footnotes Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> &Font... >> &Paragraph... >> &Style... >> &Go to Footnote >> &Note Options... >> Con&vert to Endnote >> B&lock Authors >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> &Additional Actions >> Command Bar: Frames Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Borders and Shading... >> Format Fra&me... >> Command Bar: Headings Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Decrease Indent >> &Increase Indent >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Tran&slate >> &English Assistance >> Command Bar: Linked Headings Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Object >> Download &Pictures >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> Tran&slate >> &English Assistance >> &Who Is... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Command Bar: Script Anchor Popup Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Command Bar: Lists Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Continue Numbering >> Set Numbering &Value... >> Adj&ust List Indents... >> Separate &List >> &Join To Previous List >> &Decrease Indent >> &Increase Indent >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Tran&slate >> &English Assistance >> &Additional Actions >> Command Bar: Inline Picture Visible [False] Cu&t >> &Copy >> &Paste >> &Fill in Label >> &Lock Label >> Un&lock Label >> &Sign... >> Si&gnature Details... >> Signature Setu&p... >> Remove Sig&nature >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Object >> Download &Pictures >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Insert &Caption... >> &Borders and Shading... >> Format &Object... >> Command Bar: Inline Canvas Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> Insert &Caption... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Use A&utoFormat >> Format &Object... >> Command Bar: Horizontal Line Popup Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Format &Object... >> Command Bar: Tables Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Delete Cells... >> Mer&ge/Split Cells >> Distribute Rows Eve&nly >> Distribute Columns Evenl&y >> &Borders and Shading... >> Te&xt Direction... >> Table P&roperties... >> Command Bar: Table Cells Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Delete Cells... >> Mer&ge/Split Cells >> &Borders and Shading... >> Te&xt Direction... >> Table P&roperties... >> Command Bar: Table Headings Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Delete Cells... >> Dra&w Table >> &Borders and Shading... >> &Font... >> &Paragraph... >> Command Bar: Table Lists Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> Bullets and &Numbering... >> &Decrease Indent >> &Increase Indent >> B&lock Authors >> New Co&mment >> &Delete Cells... >> Dra&w Table >> &Borders and Shading... >> &Hyperlink... >> Tran&slate >> &English Assistance >> &Font... >> &Paragraph... >> Te&xt Direction... >> &Who Is... >> &Additional Actions >> Command Bar: Table Pictures Visible [False] Cu&t >> &Copy >> &Paste >> &Sign... >> Si&gnature Details... >> Signature Setu&p... >> Remove Sig&nature >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Object >> Download &Pictures >> &Borders and Shading... >> Insert &Caption... >> &Insert Table... >> &Delete Cells... >> Dra&w Table >> &Hyperlink... >> Command Bar: Table Text Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Delete Cells... >> Mer&ge/Split Cells >> &Borders and Shading... >> Te&xt Direction... >> Table P&roperties... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> &Hyperlink... >> Tran&slate >> &English Assistance >> &Additional Actions >> Command Bar: Whole Table Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> Delete &Table >> Mer&ge/Split Cells >> Distribute Rows Eve&nly >> Distribute Columns Evenl&y >> Dra&w Table >> &Borders and Shading... >> Insert &Caption... >> Table P&roperties... >> Command Bar: Linked Table Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Delete &Table >> &Object >> Download &Pictures >> Distribute Rows Eve&nly >> Distribute Columns Evenl&y >> Table P&roperties... >> Dra&w Table >> &Borders and Shading... >> Insert &Caption... >> Command Bar: Text Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Hyperlink... >> &Who Is... >> Tran&slate >> &English Assistance >> View &Source >> &Additional Actions >> Command Bar: Word Previewer Visible [False] &Copy >> Select A&ll >> Command Bar: Linked Text Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Object >> Download &Pictures >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> Command Bar: Font Popup Visible [False] Cu&t >> &Copy >> &Paste >> &Font... >> Command Bar: Font Paragraph Visible [False] Cu&t >> &Copy >> &Paste >> &Font... >> &Paragraph... >> Command Bar: Format Inspector Popup in Normal Mode Visible [False] &Modify >> &Match Property to Formatting of Surrounding Text >> Command Bar: Format Inspector Popup in Compare Mode Visible [False] &Modify >> &Match Property to Formatting of Original Selection >> Command Bar: Spelling Visible [False] &Spelling... >> &Who Is... >> Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Additional Actions >> Command Bar: Grammar Visible [False] &Grammar... >> &Who Is... >> Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Additional Actions >> Command Bar: Grammar (2) Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Additional Actions >> Command Bar: Format consistency Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Command Bar: Background Proofing Status Bar Visible [False] Hide &Spelling Errors >> Hide &Spelling Errors >> Hide &Grammatical Errors >> &Options... >> Command Bar: Track Changes Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> Acc&ept Change >> &Reject Change >> &Follow Move >> &Track Changes >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Command Bar: Frame Properties Visible [False] Frame &Properties... >> Command Bar: Hyperlink Context Menu Visible [False] Cu&t >> &Copy >> &Paste >> Download &Pictures >> &Properties >> R&emove Content Control >> Delete Co&mment >> Edit &Hyperlink... >> &Select Hyperlink >> &Open Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> &Additional Actions >> Command Bar: AutoSignature Popup Visible [False] &Signatures... >> Command Bar: Field AutoText Visible [False] &Create AutoText... >> Command Bar: Document Map Visible [False] &Document Map >> &Expand >> &Collapse >> Show Heading &1 >> Show Heading &2 >> Show Heading &3 >> Show Heading &4 >> Show Heading &5 >> Show Heading &6 >> Show Heading &7 >> Show Heading &8 >> Show Heading &9 >> A&ll >> Command Bar: Shapes Visible [False] Cu&t >> &Copy >> Copy Ink As &Text >> &Paste >> Download &Pictures >> Delete Co&mment >> Edit Te&xt >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: Curve Visible [False] Cu&t >> &Copy >> Copy Ink As &Text >> &Paste >> Delete Co&mment >> &Edit Points >> Ope&n Path >> C&lose Path >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: Curve Node Visible [False] &Add Point >> De&lete Point >> Ope&n Path >> C&lose Path >> Au&to Point >> &Smooth Point >> St&raight Point >> &Corner Point >> Exit &Edit Point >> Command Bar: Curve Segment Visible [False] &Add Point >> De&lete Segment >> Ope&n Path >> C&lose Path >> &Straight Segment >> &Curved Segment >> Exit &Edit Point >> Command Bar: Floating Picture Visible [False] Cu&t >> &Copy >> &Paste >> Download &Pictures >> &Fill in Label >> &Lock Label >> Un&lock Label >> &Sign... >> Si&gnature Details... >> Signature Setu&p... >> Remove Sig&nature >> Delete Co&mment >> &Object >> Download &Pictures >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: Canvas Popup Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Fi&t >> Ex&pand >> S&cale Drawing >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Use A&utoFormat >> Insert &Caption... >> Format &Object... >> Command Bar: OLE Object Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Object >> Download &Pictures >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: ActiveX Control Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Propert&ies >> &View Code >> &Object >> Download &Pictures >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Format &Object... >> Command Bar: WordArt Context Menu Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Edit Te&xt... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: Rotate Mode Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Exit Rotate Mode >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: Comment Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> &Edit Comment >> Delete Co&mment >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> &Who Is... >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> &Additional Actions >> Command Bar: Organization Chart Popup Visible [False] Edit Te&xt >> &Subordinate >> &Coworker >> &Assistant >> &Delete >> Use A&utoFormat >> Format &Object... >> Command Bar: Diagram Visible [False] Edit Te&xt >> I&nsert Shape >> &Delete Shape >> Use A&utoFormat >> Format &Object... >> Command Bar: Connector Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Straight Connector >> &Elbow Connector >> C&urved Connector >> Rerou&te Connectors >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Set AutoShape &Defaults >> Format &Object... >> Command Bar: Track Changes Indicator Visible [False] &Track Changes >> Reviewing Pane >> &Options... >> Command Bar: Chinese Translation Visible [False] Translate to &Simplified Chinese >> Translate to &Traditional Chinese >> Command Bar: Address Block Popup Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Update Field >> &Edit Address Block... >> &Toggle Field Codes >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> Command Bar: Greeting Line Popup Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> &Update Field >> &Edit Greeting Line... >> &Toggle Field Codes >> &Font... >> &Paragraph... >> Bullets and &Numbering... >> B&lock Authors >> New Co&mment >> Command Bar: Inline ActiveX Control Visible [False] Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Propert&ies >> &View Code >> &Object >> Download &Pictures >> &Hyperlink... >> Edit &Hyperlink... >> &Open Hyperlink >> &Select Hyperlink >> &Copy Hyperlink >> &Remove Hyperlink >> Format &Object... >> Command Bar: XML Structure Node Popup Visible [False]Command Bar: Word CoAuthoring Visible [False] B&lock Authors >> New Co&mment >> Command Bar: XML Error Options Visible [False] &Properties >> R&emove Content Control >> Cu&t >> &Copy >> &Paste >> Delete Co&mment >> Command Bar: Ink Comment Visible [False] &Copy >> &Paste >> Delete Co&mment >> Command Bar: Business Card Visible [False] &Add to Outlook Contacts >> Command Bar: Equation Popup Visible [False] Cu&t >> &Copy >> &Paste >> &Professional >> &Linear >> &Save as New Equation... >> &Font... >> &Paragraph... >> Command Bar: Header Area Popup Visible [False] Edit &Header >> Command Bar: Footer Area Popup Visible [False] &Edit Footer >> Command Bar: Text Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Who Is... >> Tran&slate >> &English Assistance >> View &Source >> &Additional Actions >> Command Bar: Tables Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> Command Bar: Table Cells Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> Command Bar: Table Lists Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> Tran&slate >> &English Assistance >> &Who Is... >> &Additional Actions >> Command Bar: Table Pictures Visible [False] &Copy >> &Save as Picture... >> Download &Pictures >> &Sign... >> Si&gnature Details... >> Signature Setu&p... >> Remove Sig&nature >> &Properties >> R&emove Content Control >> Delete Co&mment >> Command Bar: Table Text Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Who Is... >> Tran&slate >> &English Assistance >> &Additional Actions >> Command Bar: Whole Table Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> Command Bar: Linked Table Visible [False] &Copy >> Delete Co&mment >> &Object >> Download &Pictures >> Command Bar: Lists Visible [False] &Copy >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Who Is... >> &Select Hyperlink >> &Open Hyperlink >> &Copy Hyperlink >> Tran&slate >> &English Assistance >> &Additional Actions >> Command Bar: Hyperlink Context Menu Visible [False] &Copy >> Download &Pictures >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Select Hyperlink >> &Open Hyperlink >> &Copy Hyperlink >> &Who Is... >> Command Bar: Locked Reading Mode Visible [False] &Copy >> Tran&slate >> &Additional Actions >> Command Bar: Word Page Numbering Options Visible [False] Cu&t >> &Copy >> &Paste >> &Properties >> R&emove Content Control >> Delete Co&mment >> &Edit Citation >> Edi&t Source >> &Update Field >> &Edit Field... >> &Toggle Field Codes >> &Format Page Numbers... >> &Font... >> Command Bar: AutoShapes Visible [False] &More AutoShapes... >> Command Bar: Callouts Visible [False] &Rectangular Callout >> &Rounded Rectangular Callout >> &Oval Callout >> &Cloud Callout >> &Line Callout 1 >> &Line Callout 2 >> &Line Callout 3 >> &Line Callout 4 >> &Line Callout 1 (Accent Bar) >> &Line Callout 2 (Accent Bar) >> &Line Callout 3 (Accent Bar) >> &Line Callout 4 (Accent Bar) >> &Line Callout 1 (No Border) >> &Line Callout 2 (No Border) >> &Line Callout 3 (No Border) >> &Line Callout 4 (No Border) >> &Line Callout 1 (Border and Accent Bar) >> &Line Callout 2 (Border and Accent Bar) >> &Line Callout 3 (Border and Accent Bar) >> &Line Callout 4 (Border and Accent Bar) >> Command Bar: Flowchart Visible [False] &Flowchart: Process >> &Flowchart: Alternate Process >> &Flowchart: Decision >> &Flowchart: Data >> &Flowchart: Predefined Process >> &Flowchart: Internal Storage >> &Flowchart: Document >> &Flowchart: Multidocument >> &Flowchart: Terminator >> &Flowchart: Preparation >> &Flowchart: Manual Input >> &Flowchart: Manual Operation >> &Flowchart: Connector >> &Flowchart: Off-page Connector >> &Flowchart: Card >> &Flowchart: Punched Tape >> &Flowchart: Summing Junction >> &Flowchart: Or >> &Flowchart: Collate >> &Flowchart: Sort >> &Flowchart: Extract >> &Flowchart: Merge >> &Flowchart: Stored Data >> &Flowchart: Delay >> &Flowchart: Sequential Access Storage >> &Flowchart: Magnetic Disk >> &Flowchart: Direct Access Storage >> &Flowchart: Display >> Command Bar: Block Arrows Visible [False] &Right Arrow >> &Left Arrow >> &Up Arrow >> &Down Arrow >> &Left-Right Arrow >> &Up-Down Arrow >> &Quad Arrow >> &Left-Right-Up Arrow >> &Bent Arrow >> &U-Turn Arrow >> &Left-Up Arrow >> &Bent-Up Arrow >> &Curved Right Arrow >> &Curved Left Arrow >> &Curved Up Arrow >> &Curved Down Arrow >> &Striped Right Arrow >> &Notched Right Arrow >> &Pentagon >> &Chevron >> &Right Arrow Callout >> &Left Arrow Callout >> &Up Arrow Callout >> &Down Arrow Callout >> &Left-Right Arrow Callout >> &Up-Down Arrow Callout >> &Quad Arrow Callout >> &Circular Arrow >> Command Bar: Stars & Banners Visible [False] &Explosion 1 >> &Explosion 2 >> &4-Point Star >> &5-Point Star >> &8-Point Star >> &16-Point Star >> &24-Point Star >> &32-Point Star >> &Up Ribbon >> &Down Ribbon >> &Curved Up Ribbon >> &Curved Down Ribbon >> &Vertical Scroll >> &Horizontal Scroll >> &Wave >> &Double Wave >> Command Bar: Lines Visible [False] &Line >> &Arrow >> &Double Arrow >> &Curve >> &Freeform >> &Scribble >> Command Bar: Basic Shapes Visible [False] &Rectangle >> &Parallelogram >> &Trapezoid >> &Diamond >> &Rounded Rectangle >> &Octagon >> &Isosceles Triangle >> &Right Triangle >> &Oval >> &Hexagon >> &Cross >> &Regular Pentagon >> &Can >> &Cube >> &Bevel >> &Folded Corner >> &Smiley Face >> &Donut >> &"No" Symbol >> &Block Arc >> &Heart >> &Lightning Bolt >> &Sun >> &Moon >> &Arc >> &Double Bracket >> &Double Brace >> &Plaque >> &Left Bracket >> &Right Bracket >> &Left Brace >> &Right Brace >> Command Bar: Connectors Visible [False] St&raight Connector >> &Straight Arrow Connector >> &Straight Double-Arrow Connector >> &Elbow Connector >> &Elbow Arrow Connector >> &Elbow Double-Arrow Connector >> C&urved Connector >> &Curved Arrow Connector >> &Curved Double-Arrow Connector >> Command Bar: Fill Color Visible [False] &More Fill Colors... >> &Fill Effects... >> Command Bar: Insert Shape Visible [False] &Subordinate >> &Coworker >> &Assistant >> Command Bar: Line Color Visible [False] &More Outline Colors... >> &Patterned Lines... >> Command Bar: Drawing and Writing Pens Visible [False] Black >> Blue >> Red >> Black >> Blue >> Red >> Yellow >> Turquoise >> Pink >> Command Bar: Annotation Pens Visible [False] Black >> Blue >> Red >> Black >> Blue >> Red >> Yellow >> Turquoise >> Pink >> Command Bar: Drawing and Writing Pens Visible [False] Black >> Blue >> Red >> Black >> Blue >> Red >> Yellow >> Turquoise >> Pink >> Command Bar: Annotation Pens Visible [False] Black >> Blue >> Red >> Black >> Blue >> Red >> Yellow >> Turquoise >> Pink >> Command Bar: Align or Distribute Visible [False] Align &Left >> Align &Center >> Align &Right >> Align &Top >> Align &Middle >> Align &Bottom >> Distribute &Horizontally >> Distribute &Vertically >> Relative t&o Page >> Relative t&o Canvas >> Relative t&o Organization Chart >> Relative t&o Diagram >> Command Bar: Rotate or Flip Visible [False] Free Ro&tate >> Rotate &Left 90° >> Rotate &Right 90° >> Flip &Horizontal >> Flip &Vertical >> Command Bar: Order Visible [False] Bring to F&ront >> Send to Bac&k >> Bring &Forward >> Send &Backward >> Bring in F&ront of Text >> Send Be&hind Text >> Command Bar: Nudge Visible [False] &Up >> &Down >> &Left >> &Right >> Command Bar: Borders Visible [False] &Outside Borders >> &All Borders >> Top Bord&er >> Left Border >> Inside Hori&zontal Border >> Diagonal &Down Border >> Horizontal &Line... >> &Inside Borders >> &No Border >> &Bottom Border >> &Right Border >> Inside &Vertical Border >> Diagonal &Up Border >> Command Bar: Font Color Visible [False]Command Bar: Shading Color Visible [False]Command Bar: Cell Alignment Visible [False] &Align Top Left >> &Align Top Center >> &Align Top Right >> &Align Center Left >> &Align Center >> &Align Center Right >> &Align Bottom Left >> &Align Bottom Center >> &Align Bottom Right >> Command Bar: Text Wrapping Visible [False] &In Line with Text >> &Square >> &Tight >> Behin&d Text >> I&n Front of Text >> T&op and Bottom >> T&hrough >> &Edit Wrap Points >> Command Bar: Clipboard Visible [False]Command Bar: Envelope Visible [False]Command Bar: System Visible [False] &Restore >> &Move >> &Size >> Mi&nimize >> Ma&ximize >> &Close >> Command Bar: Online Meeting Visible [False] &Call Participant >> &Remove Participants >> &Allow Others to Edit >> &Display Chat Window >> Display &Whiteboard >> &End Meeting >> Command Bar: Task Pane Visible [False]Command Bar: Visible [False]Command Bar: Property Editor Visible [False]Command Bar: Office Clipboard Visible [False]Command Bar: XML Source Visible [False]Command Bar: Research Visible [False]Command Bar: XML Document Visible [False]Command Bar: Signatures Visible [False]Command Bar: Document Actions Visible [False]Command Bar: Clip Art Visible [False]Command Bar: Selection and Visibility Visible [False]Command Bar: Document Management Visible [False]Command Bar: Document Updates Visible [False]Command Bar: Mail Merge Panes Visible [False]Command Bar: Fax Service Visible [False]Command Bar: Meeting Workspace Visible [False]Command Bar: Attachment Options Visible [False]Command Bar: Accessibility Checker Visible [False]Command Bar: Visible [False]Command Bar: Status Bar Visible [True]Command Bar: Ribbon Visible [True]

> as you can see from the line highlighted in yellow, we've found the following entry:
    ...
    Command Bar: Menu Bar Visible [True]
    Sample Menu >>
    ....

You can now use the following code to list the controls and their callbacks:

Sub test()Dim cmd As CommandBarDim ctr As CommandBarControlSet cmd = Application.CommandBars("Menu Bar")Set ctr = cmd.Controls("SampleMenu")

On Error Resume Next For Each ctrl In ctr.Controls Debug.Print " " & ctrl.Caption & " " For Each sub_ctrl In ctrl.Controls Debug.Print " >> Caption: " & sub_ctrl.Caption & vbTab & vbTab; " OnAction: " & sub_ctrl.OnAction     Next NextEnd Sub 

Output ==========================================================================  Project.Module1.Macro1 Sample SubMenu >> Caption: Project.Module1.Macro2 OnAction: Module1.Macro2 >> Caption: Project.Module1.Macro3 OnAction: Module1.Macro3

If you put a breakpoint after the ctr object gets instantiated, you can expand it in Watch and take a closer look at the controls which are embedded. For example, here we see it holds a CommandBarButton control in the Controls collection at Item 1, and at Item 2 it holds a CommandBarPopup control which in turn is embedding the other two buttons. 

 

How to add a new button ?

Please find this code from below which gets a link to the 'Application.CommandBars("Menu Bar")' object (which represents the old main menu in Office 2003), then it defines a 'CommandBarControl' object which points to the 'SampleMenu' component inside this collection. For demonstration purposes I have added a 'CommandBarPopup' and a 'CommandBarButton' inside.

'==============================================================' * Please note that Microsoft provides programming ' * examples for illustration only, without' * warranty either expressed or implied, including, ' * but not limited to, the implied warranties ' * of merchantability and/or fitness ' * for a particular purpose. Any use by you of the code ' * provided in this blog is at your own risk. '===============================================================

Sub test_cmd()Dim cmd As CommandBarDim ctr As CommandBarControl

Set cmd = Application.CommandBars("Menu Bar")Set ctr = cmd.Controls("SampleMenu")

Dim myControl As CommandBarPopup

'Dim Type As Object | Dim Id As Object | Dim Parameter As Object | Dim Before As Object | Dim Temporary As ObjectSet myControl = ctr.Controls.Add(msoControlPopup, , , , False)With myControl  .Caption = "Pop-up1"  .Visible = TrueEnd With

Dim myButton As CommandBarButton

Set myButton = myControl.Controls.Add(msoControlButton, , , , False)

With myButton   .Caption = "Click me!"   .Style = msoButtonIconAndCaption   .FaceId = 5941   .OnAction = "Sub_Itworks"End WithEnd Sub

Sub Sub_Itworks()   MsgBox "test"End Sub

 
CAUTION: do not run this code more than once, or the controls will get duplicated;

Since you know how to obtain a reference to the built-in CommandBar menu, you can now freely add and delete controls from its collection.

 

Thank you for reading my article! Bye :-)