Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference to submenu

Status
Not open for further replies.

PeteR61

Technical User
Apr 29, 2006
4
0
0
NL
Hello, Basically I want to access an existing menu to add (sub)menu item: e.g. to access File CommandBarControl.

         Set fileCtrl = Application.CommandBars("Menu Bar").Controls(1)  'File menu

But how do I get a reference to  the other elements in the menu:

       Set newCtrl = fileCtrl.Controls(1)

As far as I understood if CommandBarControl is of type msoControlPopup(CommandBarPopup)  then this CommandBarControl has a Controls property. But I can't get anything going in this respect. There doesn't seem to exist a Controls property.

Any help welcome here!!!
==================================


By the way I found excel code that does something similar with the Help menu. It works ok in powerpoint but I am puzzled that it DOES work
:
Sub Auto_Open()

Dim lngPos As Long
Dim objHelpMenu As CommandBar
Dim objHelpMenuItem As CommandBarControl
Dim objExcelAbout As CommandBarControl

'Get reference to Help menu
 Set objHelpMenu = Application.CommandBars("Help")

'???>> how come Help being a CommandBar????????????????????????? when I check each control type on the menubar I get returned type number 10:msoControlPopup<<'

'Determine the position of "About Microsoft Office Powerpoint"
Set objExcelAbout = objHelpMenu.Controls("About Microsoft Office Powerpoint")
If Not objExcelAbout Is Nothing Then
    lngPos = objExcelAbout.Index
Else
    lngPos = objHelpMenu.Controls.Count
End If

'add "myItemGoesHere"menu item
Set objHelpMenuItem = objHelpMenu.Controls.Add(msoControlButton, 1, , _
                        lngPos, True)
objHelpMenuItem.Caption = "myItemGoesHere"
objHelpMenuItem.BeginGroup = True

exit sub
 
But how do I get a reference to the other elements in the menu:

Set newCtrl = fileCtrl.Controls(1)
...
There doesn't seem to exist a Controls property.
*** I'll assume you are working in Powerpoint. ***
That line works for me. It returns a reference to the New... CommandBarButton. I'm not sure if you are trying to add a new control to the File menu or to a popup menuitem on the File menu. Here is some example code for either situation. The first adds a new CommandBarButton control to the Send To popup control:
Code:
Sub CustomizeMenu()
Dim fileCtrl As CommandBarControl
Dim SendToCtrl As CommandBarControl
Dim newCtrl As CommandBarButton

   Set fileCtrl = Application.CommandBars("Menu Bar").Controls(1)

   [COLOR=green]' The following line sets a reference to the 'Send To' menuitem
   ' but assumes it is always at Index = 18[/color]
   Set SendToCtrl = fileCtrl.Controls(18)
   [COLOR=green]' The following is a surer way to reference a particular
   ' control[/color]
   Set SendToCtrl = Application.CommandBars("Menu Bar").FindControl(Id:=30095, recursive:=True) 'fileCtrl.Controls(19)
   Set newCtrl = SendToCtrl.Controls.Add(msoControlButton, 1, , 1, True)
   With newCtrl
     .Style = msoButtonIconAndCaption
     .Caption = "My Secret Location"
     .FaceId = 926
     .OnAction = "MyCustomSendTo"
   End With

End Sub
The next example adds a new control of type msoControlPopup just before the Send To menuitem and adds one CommandBarButton control to it:
Code:
Sub AddPopupMenu()
Dim fileCtrl As CommandBarControl
Dim SendToCtrl As CommandBarControl
Dim newCtrl As CommandBarControl
Dim newBtn As CommandBarButton

   Set fileCtrl = Application.CommandBars("Menu Bar").Controls(1)
   
   Set SendToCtrl = Application.CommandBars("Menu Bar").FindControl(Id:=30095, recursive:=True) 'fileCtrl.Controls(19)
   Set newCtrl = fileCtrl.Controls.Add(msoControlPopup, 1, , SendToCtrl.Index, True)
   With newCtrl
     .BeginGroup = True
     .Caption = "My Custom Popup"
   End With
[COLOR=green]' Repeat the following block for each menuitem
  you want on the custom popup menu[/color]
   Set newBtn = newCtrl.Controls.Add(msoControlButton)
   With newBtn
     .Style = msoButtonCaption
     .Caption = "MenuItem 1"
     .OnAction = "MenuItem1_Procedure"
   End With
End Sub

Hope this helps.

As to the example code, that one has me scratching my head also. It worked for me, too.


Regards,
Mike
 
Thanks, great response!! Yes my code refers to powerpoint.

Thanks

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top