Group:  Microsoft Word ยป microsoft.public.word.customization.menustoolbars
Thread: Identify code linked to custom menu.

Geek News

Identify code linked to custom menu.
kbaxreno 12/15/2008 10:49:01 PM
Hello All.
I have adopted a huge application written in Word VBA. There are probably
50 custom menus and submenus. I understand VB and have no problem
maintaining and updating the code, but I have not figured out how to identify
the template and module that is linked to a custom menu without making
guesses in the code, installing breakpoints, crossing my fingers and running
the apps. Often this takes me a few tries. As I become more familiar with
the project my guesses are much better but it seems there must be a better
way.

Thanks Ahead...
Ken

Re: Identify code linked to custom menu.
Jay Freedman <jay.freedman[ at ]verizon.net> 12/16/2008 1:47:18 AM
On Mon, 15 Dec 2008 14:49:01 -0800, kbaxreno
<kbaxreno[ at ]discussions.microsoft.com> wrote:

[Quoted Text]
>Hello All.
>I have adopted a huge application written in Word VBA. There are probably
>50 custom menus and submenus. I understand VB and have no problem
>maintaining and updating the code, but I have not figured out how to identify
>the template and module that is linked to a custom menu without making
>guesses in the code, installing breakpoints, crossing my fingers and running
>the apps. Often this takes me a few tries. As I become more familiar with
>the project my guesses are much better but it seems there must be a better
>way.
>
>Thanks Ahead...
>Ken

These macros will give you a hint, although they'll still leave you doing some
poking around.

Sub MacrosOnToolbars()
Dim oCB As CommandBar
Dim oCBC As CommandBarControl
CustomizationContext = NormalTemplate

For Each oCB In CommandBars
For Each oCBC In oCB.Controls
On Error Resume Next
With oCBC
If Len(.OnAction) Then
MsgBox .Caption & " uses " & .OnAction
End If
End With
Next
Next
End Sub

Sub MacrosOnMenus()
Dim oCB As CommandBarControl
Dim oCBC As CommandBarControl
CustomizationContext = NormalTemplate

For Each oCB In CommandBars("Menu Bar").Controls
For Each oCBC In oCB.Controls
On Error Resume Next
With oCBC
If Len(.OnAction) Then
MsgBox .Caption & " uses " & .OnAction
End If
End With
Next
Next
End Sub

If the toolbars and menus are stored in some template other than Normal.dot,
you'll need to change the CustomizationContext to that template.

The message box will tell you the caption (the display in a menu, or the tooltip
of a toolbar button) and the name of the module and macro that are assigned to
the command. It won't tell you the name of the template that contains the
module; and you won't see the module in the VBA editor's project pane if the
template is a global one. You can see the module names in the Organizer, or you
can open the global templates through the File > Open command and then view them
in the VBA editor.


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Re: Identify code linked to custom menu.
kbaxreno 12/16/2008 4:33:01 PM
Thanks for the code and the answer Jay.

"Jay Freedman" wrote:

[Quoted Text]
> On Mon, 15 Dec 2008 14:49:01 -0800, kbaxreno
> <kbaxreno[ at ]discussions.microsoft.com> wrote:
>
> >Hello All.
> >I have adopted a huge application written in Word VBA. There are probably
> >50 custom menus and submenus. I understand VB and have no problem
> >maintaining and updating the code, but I have not figured out how to identify
> >the template and module that is linked to a custom menu without making
> >guesses in the code, installing breakpoints, crossing my fingers and running
> >the apps. Often this takes me a few tries. As I become more familiar with
> >the project my guesses are much better but it seems there must be a better
> >way.
> >
> >Thanks Ahead...
> >Ken
>
> These macros will give you a hint, although they'll still leave you doing some
> poking around.
>
> Sub MacrosOnToolbars()
> Dim oCB As CommandBar
> Dim oCBC As CommandBarControl
> CustomizationContext = NormalTemplate
>
> For Each oCB In CommandBars
> For Each oCBC In oCB.Controls
> On Error Resume Next
> With oCBC
> If Len(.OnAction) Then
> MsgBox .Caption & " uses " & .OnAction
> End If
> End With
> Next
> Next
> End Sub
>
> Sub MacrosOnMenus()
> Dim oCB As CommandBarControl
> Dim oCBC As CommandBarControl
> CustomizationContext = NormalTemplate
>
> For Each oCB In CommandBars("Menu Bar").Controls
> For Each oCBC In oCB.Controls
> On Error Resume Next
> With oCBC
> If Len(.OnAction) Then
> MsgBox .Caption & " uses " & .OnAction
> End If
> End With
> Next
> Next
> End Sub
>
> If the toolbars and menus are stored in some template other than Normal.dot,
> you'll need to change the CustomizationContext to that template.
>
> The message box will tell you the caption (the display in a menu, or the tooltip
> of a toolbar button) and the name of the module and macro that are assigned to
> the command. It won't tell you the name of the template that contains the
> module; and you won't see the module in the VBA editor's project pane if the
> template is a global one. You can see the module names in the Organizer, or you
> can open the global templates through the File > Open command and then view them
> in the VBA editor.
>
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
>
RE: Identify code linked to custom menu.
kbaxreno 12/19/2008 11:42:21 PM

Jay led me in the right direction, working with the commandbars. I made
some changes to his code to follow submenus and print the results in a file.


Function getallsubmenunames()
Dim loop1, loop2 As Long
Dim cbarMenu As CommandBar
Dim cbarmain As CommandBarControl
Dim cbarcontrol As CommandBarControl
Dim cbarcontrol2 As CommandBarControl
Set cbarMenu = CommandBars("menu bar")
For Each cbarmain In cbarMenu.Controls
loop2 = loop2 + 1
loop1 = 0
LogInformation cbarMenu.Controls(loop2).Caption & " - " &
cbarMenu.Controls(loop2).DescriptionText
For Each cbarcontrol In cbarMenu.Controls(loop2).Controls
loop1 = loop1 + 1
If cbarcontrol.Type = 10 Then
LogInformation " " & cbarcontrol.Caption
For Each cbarcontrol2 In
cbarMenu.Controls(loop2).Controls(loop1).Controls
If cbarcontrol2.BuiltIn Then
LogInformation " " & " " & cbarcontrol2.Caption
Else
LogInformation " " & " " & cbarcontrol2.Caption & " uses" &
" " & cbarcontrol2.OnAction
End If
Next
Else
If cbarcontrol.BuiltIn Then
LogInformation " " & cbarcontrol.Caption
Else
LogInformation " " & cbarcontrol.Caption & " uses" & " " &
cbarcontrol.OnAction
End If
End If
Next
Next
End Function

Sub LogInformation(LogMessage As String)
Const LogFileName As String = "C:\TempTextFile.txt"
Dim FileNum As Integer
FileNum = FreeFile ' next file number
Open LogFileName For Append As #FileNum ' creates the file if it doesn't
exist
Print #FileNum, LogMessage ' write information at the end of the text file
Close #FileNum ' close the file
End Sub


"kbaxreno" wrote:

[Quoted Text]
> Hello All.
> I have adopted a huge application written in Word VBA. There are probably
> 50 custom menus and submenus. I understand VB and have no problem
> maintaining and updating the code, but I have not figured out how to identify
> the template and module that is linked to a custom menu without making
> guesses in the code, installing breakpoints, crossing my fingers and running
> the apps. Often this takes me a few tries. As I become more familiar with
> the project my guesses are much better but it seems there must be a better
> way.
>
> Thanks Ahead...
> Ken
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net