Commandbarpopup error in Word VBA

O

OughtFour

I use the following routine to test each menu and toobar control, visible or
not:

Private Sub EnableMenuette(vControl As Variant, vName As String, IsADoc As
Boolean)
'Does heavy lifting for EnableMenuItem, recursively
Dim thang As Variant
For Each thang In vControl.Controls
If thang.Caption = vName Then
thang.Enabled = IsADoc
ElseIf thang.Type = msoControlPopup Then
'i.e., if thang is itself a menu,
EnableMenuette thang, vName, IsADoc
End If
Next thang
End Sub

It uses recursion to get at sub menus. It is called by a routine that
includes these lines

For Each menu In CommandBars
EnableMenuette menu, vName, IsADoc
Next menu

(What I am doing is checking for the presence of particular toolbar button
and menu item vName, which may be anywhere and in more than one place due to
user customization.)

This works fine in Word 97 for Windows, but fails in Word X. The error
message says

Run-time error '-2147483640 (800000080)':
Method 'Controls' of object 'CommandBarPopup' failed.

Execution halts at
For Each thang In vControl.Controls

It seems to be hung up when it reaches the Toolbars submenu on the View
menu.

I surmise that the problem lies in applying the Controls method.

I feel baffled by this. I thought I understood the CommandBars collection
well enought to do this but apparently I do not.

But, why does it work in Word 97?

Grateful for any help at all, thanks.
 
K

Klaus Linke

It seems to be hung up when it reaches the Toolbars submenu on the
View menu.


Perhaps you could code around it:

If vControl.ID <> 30045 Then ' 30045 being the ID of "View > Toolbars"
For Each thang In vControl.Controls
' rest of Sub EnableMenuette
' ...
End If

(You could also use vControl.Caption, but I'm not quite sure about its
Caption in English)

Regards,
Klaus
 
O

OughtFour

Klaus said:
Perhaps you could code around it:

If vControl.ID <> 30045 Then ' 30045 being the ID of "View > Toolbars"
For Each thang In vControl.Controls
' rest of Sub EnableMenuette
' ...
End If

Thanks, Klaus.

The error seems confined to "View > Toolbars."

I'd still like to understand what is wrong with the original code, but
this is a fine workaround.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top