2

I have a Word 2003 toolbar made by someone else long time ago. The toolbar contains lots of buttons and assigned macros.

Until now I have been able to set breakpoints in the VBA code in order to figure out which macros are run, but for one button in my toolbar I have a problem. There does not seem to be any corresponding macro. When I click on the toolbar button, Word also says "The macro cannot be found ..."). My breakpoint finding strategy doesn't seem to a winning concept here.

I am very well aware of this: Tools menu > Customize > Right click on the specific toolbar button:

Tools menu > Customize > Right click on the specific toolbar button

Unfortunately this doesn't help me. Can I, somehow, see callback event properties for a certain toolbar button? I need to know which macro a certain button is intended to run.

1 Answer 1

1

Prints out all macro names which are assigned to any menu button

Sub ReadBack_Buttons()
    On Error Resume Next        
    '## Loop through every menu bar
    For Each bar In Application.CommandBars        
        '## Loop through every button on the current menu bar
        For Each button In bar.Controls            
            '## If a macro is assigned, print it out
            If button.OnAction <> "" Then Debug.Print button.Caption & " = " & button.OnAction                
            '## Loop through every button on dropdown menus
            For Each subbutton In button.Controls                
                '## If a macro is assigned, print it out
                If subbutton.OnAction <> "" Then Debug.Print subbutton.Caption & " = " & subbutton.OnAction                    
            Next
        Next
    Next
End Sub

For a quick test, add your own custom menu with this second macro.

Sub CreateCommandBar()
    On Error Resume Next        
'## Delete the commandbar if it exists
    Application.CommandBars("example").Delete       
'## Create a new Command Bar
    Set bar = CommandBars.Add(Name:="example", Position:=msoBarFloating)
    bar.Visible = True       
'## Add popup menu
    Set menu1 = bar.Controls.Add(Type:=msoControlPopup)
    menu1.Caption = "My custom menu"       
'## Add button 1 to popup menu
    Set Btn2 = menu1.Controls.Add(Type:=msoControlButton)
    Btn2.Caption = "missing macro assigned"
    Btn2.OnAction = "Not_working_dummy"       
'## Add button 2 to popup menu
    Set Btn2 = menu1.Controls.Add(Type:=msoControlButton)
    Btn2.Caption = "Hello World"
    Btn2.OnAction = "Hello_world"        
End Sub

Sub Hello_world()
    MsgBox "Hey, it works"
End Sub

After you've executed CreateCommandBar you will see a new entry on your main menu. One with a working macro assigned and one without

enter image description here

Now, run the first macro ReadBack_Buttons and have a look at the immediate pane

missing macro assigned = Not_working_dummy  
Hello World = Hello_world
3
  • Hmmm... Does this method also work if the toolbar (CommandBar) was made at design time? (I mean: Using the toolbar designer tool in Word 2003?) I have full access to the entire VBA code in the Word 2003 template. Unfortunately there is nothing like CommandBars(index).Controls.xxx
    – Hauns TM
    Commented Mar 10, 2014 at 20:51
  • @HaunsTM Oh sorry, I confused Excel and Word. Fixed it and worked out a better solution. Hope this helps
    – nixda
    Commented Mar 11, 2014 at 2:59
  • Just tried this as I believe to have (almost) the same problem: I created a custom menu item In Word a long time ago that has a macro associated to it. Now I need to find out which macro this is. I just tried the above macro Sub ReadBack_Buttons(), however this does not produce any output at all (in Word 2003).
    – David.P
    Commented Jul 29, 2022 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .