I am following some code out an Excel manual to add a menu item to the command bar when I open my Excel application. The code gives me an error (Run time error '91' - Object Variable or With block variable not set) at the following line -
Set ViewMenu = CommandBars(1).FindControl(ID:=30010)
According to the manual, the ID property for the menu item 'View' is 30004. I am trying to add the menu item "DRT" between the Edit and View commands.
Do I need to set a reference for an additional library?
Currently I have -
VB for Applications
MS Excel 9.0 Object Library
OLE Automation
MS Office 9.0 Object Library
My code is as follows -
Sub CreateMenu()
Dim ViewMenu As CommandBarControl
Dim NewMenu As CommandBarPopup
' Delete the menu if it already exits.
Call DeleteMenu
' Find the View Menu
Set ViewMenu = CommandBars(1).FindControl(ID:=30010)
If ViewMenu Is Nothing Then
' Add the menu to the end.
Set NewMenu = CommandBars(1).Controls _
.Add(Type:=msoControlPopup, temporary:=True)
Else
' Add the menu before View
Set NewMenu = CommandBars(1).Controls _
.Add(Type:=msoControlPopup, before:=ViewMenu.Index, _
temporary:=True)
End If
' Add a caption for the menu
NewMenu.Caption = "D&RT"
' FIRST MENU ITEM
Set MenuItem = NewMenu.Controls.Add _
(Type:=msoControlButton)
With MenuItem
.Caption = "Insert &Bit.."
.OnAction = "addbit"
End With
End Sub
Any help would be appreciated.
Thanks for your help in advance.
BusMgr
Set ViewMenu = CommandBars(1).FindControl(ID:=30010)
According to the manual, the ID property for the menu item 'View' is 30004. I am trying to add the menu item "DRT" between the Edit and View commands.
Do I need to set a reference for an additional library?
Currently I have -
VB for Applications
MS Excel 9.0 Object Library
OLE Automation
MS Office 9.0 Object Library
My code is as follows -
Sub CreateMenu()
Dim ViewMenu As CommandBarControl
Dim NewMenu As CommandBarPopup
' Delete the menu if it already exits.
Call DeleteMenu
' Find the View Menu
Set ViewMenu = CommandBars(1).FindControl(ID:=30010)
If ViewMenu Is Nothing Then
' Add the menu to the end.
Set NewMenu = CommandBars(1).Controls _
.Add(Type:=msoControlPopup, temporary:=True)
Else
' Add the menu before View
Set NewMenu = CommandBars(1).Controls _
.Add(Type:=msoControlPopup, before:=ViewMenu.Index, _
temporary:=True)
End If
' Add a caption for the menu
NewMenu.Caption = "D&RT"
' FIRST MENU ITEM
Set MenuItem = NewMenu.Controls.Add _
(Type:=msoControlButton)
With MenuItem
.Caption = "Insert &Bit.."
.OnAction = "addbit"
End With
End Sub
Any help would be appreciated.
Thanks for your help in advance.
BusMgr