Eric,
Your code looks fine (minus the "Worksheet Menu Bar"); however, if you're
simply running off of what is "visible" via the menu drop-down, then there
could potentially be a misspelling, missing space, missing "...", etc. I
tried putting together a recursive process to loop through the CommandBars
object, but since objects are passed ByRef, I couldn't get the results I was
looking for in a timely manner. I decided to forego the recursive object
approach and use the "three-level" system. As far as I know, Excel 2003 can
handle only three sub-menu levels. The code below reflects this idea (though
not fully tested). The code below is giving me some unexpected results;
however, it should work for now (without me spending more time on it).
Run "MenuLoop" and see what comes up under your "MyBar" menu. Keep in mind
that I added some leading spaces before each .Caption to create indenting. I
used three spaces for the first level, six spaces for the second level, nine
.... Again, check the spelling.
Also, try doing the following (i.e. add the "Worksheet Menu Bar"):
Set cBarBtn =
Application.CommandBars("Worksheet Menu
Bar").Controls("MyBar").Controls("ABC").Controls("&Download / Refresh
Data").Controls("&Selected Cell(s)")
Best,
Matt
Sub MenuLoop()
Dim CtrlsMenu As CommandBarControls
Dim CtrlsSubMenuOne As CommandBarControls
Dim CtrlsSubMenuTwo As CommandBarControls
Dim CtrlsSubMenuThr As CommandBarControls
Dim lngCntMenu As Long
Dim lngCntSubMenuOne As Long
Dim lngCntSubMenuTwo As Long
Dim lngCntSubMenuThr As Long
Dim cBar As CommandBar
Dim strCaption As String
Dim Wks As Worksheet
Dim lngCnt As Long
Dim intCnt As Integer
Set Wks = Worksheets.Add
Set cBar = Application.CommandBars("Worksheet Menu Bar")
intCnt = 0
With Wks
'loop menus
For lngCntMenu = 1 To cBar.Controls.Count
'Debug.Print "cBar.Name:"; cBar.Name
lngCnt = 1
intCnt = intCnt + 1
.Cells(lngCnt, intCnt).Value = cBar.Controls(lngCntMenu).Caption
lngCnt = lngCnt + 1
Set CtrlsMenu = cBar.Controls(lngCntMenu).Controls
'loop sub menu (1st level)
For lngCntSubMenuOne = 1 To CtrlsMenu.Count
strCaption = CtrlsMenu(lngCntSubMenuOne).Caption
If strCaption <> "" Then
.Cells(lngCnt, intCnt).Value = " " & strCaption
lngCnt = lngCnt + 1
End If
On Error Resume Next
Set CtrlsSubMenuOne = CtrlsMenu(lngCntSubMenuOne).Controls
On Error GoTo 0
If Not CtrlsSubMenuOne Is Nothing Then
'loop sub menu (2nd level)
For lngCntSubMenuTwo = 1 To CtrlsSubMenuOne.Count
strCaption = CtrlsSubMenuOne(lngCntSubMenuTwo).Caption
If strCaption <> "" Then
.Cells(lngCnt, intCnt).Value = " " & strCaption
lngCnt = lngCnt + 1
End If
On Error Resume Next
Set CtrlsSubMenuTwo =
CtrlsSubMenuOne(lngCntSubMenuTwo).Controls
On Error GoTo 0
If Not CtrlsSubMenuTwo Is Nothing Then
'loop sub menu (3rd level)
For lngCntSubMenuThr = 1 To CtrlsSubMenuTwo.Count
strCaption =
CtrlsSubMenuTwo(lngCntSubMenuThr).Caption
If strCaption <> "" Then
.Cells(lngCnt, intCnt).Value = " " &
strCaption
lngCnt = lngCnt + 1
End If
Next lngCntSubMenuThr
End If
Next lngCntSubMenuTwo
End If
Next lngCntSubMenuOne
Next lngCntMenu
End With
Set CtrlsMenu = Nothing
Set CtrlsSubMenuOne = Nothing
Set CtrlsSubMenuTwo = Nothing
Set CtrlsSubMenuThr = Nothing
Set cBar = Nothing
Set Wks = Nothing
End Sub