Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Techguy Sr eng.   on Mar 13 In MS Office Category.

  
Question Answered By: Kristin Johnston   on Mar 13

I recommend that you use MSDN online as a reference:

msdn.microsoft.com/.../\
ofcmdbar.asp

There are frequently examples of the selected
Object/Method/Property. They may take some modifications to work in
your situation.


You can certainly add items to the standard menus  and add menus to
the standard menu  bar. I may be wrong, but I'm pretty sure you can
even disable the standard menu bar and add your own.

I hope some examples help. Here are some examples (from my
working code) for adding (and, upon exit, removing) menus. They may
not be pretty, but they work:

The first Sub adds an item to a standard Edit menu and adds two
additional menus to the menu bar. The firstadded menu is a blank menu
to make the second added menu name stands out.

The second Sub removes any and all menus with the specific name(s). I
had a problem that caused the menu to be added more than once and
needed that algorithm.

The third Sub only removes the added menus.


Sub AddKenwoodMenu(Optional h As Byte) ' From MSDN then adapted
Debug.Print " ThisWorkbk 5 Add Menu"
' Adds UnDoSorts and TH-F6A menus
Dim MB As Object
Dim MN As Object
Application.Cursor = xlWait

Set MB = MenuBars("Worksheet")

MB.Menus("Edit").MenuItems.Add Caption:="U&nDoAllSorts",
before:="-", OnAction:="UnDoSorts"

MB.Menus.Add Caption:=" " ' Adds a top level menu called
" ".
MB.Menus.Add Caption:="TH-F6&A" ' Adds a top level menu called
"TH-F6&A".
MB.Menus("&TH-F6A").MenuItems.Add Caption:="&New...",
OnAction:="Utilities.ClearData"
MB.Menus("&TH-F6A").MenuItems.Add Caption:="&Open .Fx...",
OnAction:="FileRead.ReadFxFile" ' Adds an item called "&Open .FX"
under the menu "Kenwood"
MB.Menus("&TH-F6A").MenuItems.Add Caption:="Save&As .Fx ...",
OnAction:="FileRead.WriteFxFile" ' Adds an item called "&Save .FX"
under the menu "Kenwood"
MB.Menus("&TH-F6A").MenuItems.Add Caption:="-" ' Adds a divider
MB.Menus("&TH-F6A").MenuItems.Add Caption:="Transfer &Memories...",
OnAction:="Sheet3.MemoryTransfer_Click"
MB.Menus("&TH-F6A").MenuItems.Add Caption:="&UnDoAllSorts",
OnAction:="UnDoSorts"
End Sub

Public Sub RemoveKenwoodMenus(Optional h As Byte)
Debug.Print " ThisWorkbk 8 Remove Menus"
Dim MB As Object
Dim MN As Object
Application.Cursor = xlWait

' Delete all instances that may exist of my menus.
For Each MB In MenuBars
For Each MN In MB.Menus

If MN.Caption = "TH-F6&A" Then
MB.Menus("TH-F6&A").Delete
ElseIf MN.Caption = " " Then
MB.Menus(" ").Delete
End If

Next MN
Next MB
On Error Resume Next
' Don't test, just delete & swallow the error if not present.
MenuBars("Worksheet").Menus("Edit").MenuItems("UnDoAllSorts").Delete
On Error GoTo 0
End Sub

Public Sub RemoveKenwoodMenu(Optional h As Byte)
Debug.Print " ThisWorkbk 9 Remove Menu"
Dim MB As Object

Set MB = MenuBars("Worksheet")
MB.Menus(" ").Delete ' Removes the top level menu called
" ".
MB.Menus("TH-F6&A").Delete ' Removes the top level menu called
"Kenwood".
MB.Menus("Edit").MenuItems("UnDoAllSorts").Delete

End Sub

Share: 

 

This Question has 5 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Menus in VBA Excel Or get search suggestion and latest updates.


Tagged: