Some code thingies?
I'm pretty sure I've stolen this off a book or the net, but I can't remember from where, so I don't know who to credit, I'm afraid. Only thing I know, is, I use too much time every time I'm trying to understand it;-)
Using late binding, so no references are needed. Relevant reference would be the Office <version> Object Library
The litterals used here, are:[tt]
10 msoControlPopup
1 msoCommandButton
Public Sub CreateCustomMenu()
Dim mnuHelp As Object
Dim mnuNew As Object
Dim mnuItem As Object
Dim mnuSubItem As Object
On Error Goto CreateCustomMenu_Err
' felete menu, if it alredy exists
Call RemoveCustomMenu
' find the Help menu (id 30010), to place this "in front"
Set mnuHelp = Application.CommandBars("Menu Bar").FindControl(id:=30010)
If (mnuHelp Is Nothing) Then
' no help menu - oups... add to end
Set mnuNew = Application.CommandBars("Menu Bar").Controls.Add( _
Type:=10, temporary:=True)
Else
' add in front of help menu
Set mnuNew = Application.CommandBars("Menu Bar").Controls.Add( _
Type:=10, temporary:=True, Before:=mnuHelp.Index)
End If
mnuNew.Caption = "&My Custom"
' Adding menu items
Set mnuItem = mnuNew.Controls.Add(Type:=1)
With mnuItem
.Caption = "&First entry"
.OnAction = "SomeSub_or_function"
End With
Set mnuItem = mnuNew.Controls.Add(Type:=1)
With mnuItem
.Caption = "Se&cond entry"
.OnAction = "SomeOtherSub_or_function"
End With
' One submenu
Set mnuItem = mnuNew.Controls.Add(Type:=10)
With mnuItem
.BeginGroup = True
.Caption = "SubMenu"
End With
' Adding menu items
Set mnuSubItem = mnuItem.Controls.Add(Type:=1)
With mnuSubItem
.Caption = "&First sub entry"
.OnAction = "SomeSub_Sub_or_function"
End With
Set mnuSubItem = mnuItem.Controls.Add(Type:=1)
With mnuSubItem
.Caption = "Se&cond sub entry"
.OnAction = "SomeOtherSub_Sub_or_function"
End With
CreateCustomMenu_Exit:
Set mnuHelp = Nothing
Set mnuNew = Nothing
set mnuItem = Nothing
Set mnuSubItem = Nothing
Exit Sub
CreateCustomMenu_Err:
MsgBox Err.Description
Resume CreateCustomMenu_Exit
End Sub
Sub RemoveCustomMenu()
On Error Resume Next
Application.CommandBars("Menu Bar").Controls("&My Custom").Delete
End Sub
[/tt]
Roy-Vidar