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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem inserting a menu item to command bar 1

Status
Not open for further replies.

BusMgr

IS-IT--Management
Aug 21, 2001
138
US
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
 
Thanks Skip

I was reading another post and went back to see that you had answered me. Works great.

Thanks again.

BusMgr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top