View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_7_] Bob Phillips[_7_] is offline
external usenet poster
 
Posts: 1,120
Default problem with custom menu

No need to bother, just delete it with error handling, and then add it.

Sub RemoveMenuControl(cb As CommandBar, ctl As String)
On Error Resume Next
' Remove Menu Control
cb.Controls(ctl).Delete
On Error GoTo 0
End Sub


Sub addMenu()
Dim cb As CommandBar
Dim cmbmenu As CommandBarControl

Set cb = CommandBars("Worksheet Menu Bar")
RemoveMenuControl cb, "Menu1"
Set cmbmenu = cb.Controls.Add(Type:=msoControlPopup, _
Befo=CommandBars("Worksheet Menu Bar") _
.Controls.Count)
' Set the caption of the new menu.
With cmbmenu
.Caption = "menu1" 'Range("menu1")
.DescriptionText = "Macros Menu"
End With

End Sub


--
HTH

Bob Phillips

"Tony" wrote in message
...
I have found the source of my problem. In the code I was checking if the

menu
already existed to make sure it will not be multiplied. My problem is now

how
to find out if the menu which caption is setup by:

.Caption = Range("menu1")

already exist.

After checking it will be added or not as required. The second question is
how to modify the menu deleting code.

Thanks for help.

Tony

.

"Tony" wrote:

I had to modify my custom menu. Originally adding and removing my custom
menus was done by the following code:

-------------------------------------------------
Set cmbMenu = CommandBars("Worksheet Menu Bar"). _
Controls.Add(Type:=msoControlPopup, _
Befo=CommandBars("Worksheet Menu Bar") _
.Controls.Count)
' Set the caption of the new menu.
With cmbMenu
.Caption = "My Macros"
.DescriptionText = "Macros Menu"
End With
----------------------------------------------------------
Sub RemoveMenus()
On Error Resume Next
' Remove Menu Bar.
CommandBars("Worksheet Menu Bar").Controls("My Macros").Delete
End Sub
----------------------------------------------------------


Because I have to use special characters in the names of my custom menu,
following the advice from this group I modified my code to:

----------------------------------------------------------
Set cmbMenu = CommandBars("Worksheet Menu Bar"). _
Controls.Add(Type:=msoControlPopup, _
Befo=CommandBars("Worksheet Menu Bar") _
.Controls.Count)
' Set the caption of the new menu.
With cmbMenu
.Caption = Range("menu1")
.DescriptionText = "Macros Menu"
End With
----------------------------------------------------------

Now executing of the code is replacing before last menu item with my

menu.
What I am doing wrong ? How I have to modify my code so that my menu

will add
new menu, not replace one of the existing ones. How I need to modify the
removing code so that my new menu will be removed when closing the

workbook.

Thanks for advice.

Regards,

Tony