I am trying to run the code below but as soon as I start debugging the following message appears. I have checked and there appear to be no missing references. When the message appears 'cMenu1 As CommandBarControl' is highlighted blue in the code. Does anyone know the problem here? Thanks for any help!
"Compile Error:
User-defined type not defined"
Private Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
'(1)Delete any existing one.We must useOn Error Resume Next in case it does not exist.
On Error Resume Next
Application.CommandBars("Menu Bar").Controls("Import Tables").Delete
'(2)Set a CommandBar variable to Worksheet menu bar
Set cbMainMenuBar = Application.CommandBars("Menu Bar")
'(3)Return the Index number of the Help menu. We can then use this to place a custom menu before.
iHelpMenu = cbMainMenuBar.Controls("Help").Index
'(4)Add a Control to the "Worksheet Menu Bar" before Help
'Set a CommandBarControl variable to it
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)
With cbcCutomMenu
.Caption = "Import Tables"
End With
'(5)Give the control a caption cbcCutomMenu.Caption = "&New Menu"
'(6)Working with our new Control, add a sub control and give it a Caption and tell it which macro to run (OnAction).
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Import Tables"
.OnAction = "ImportTables"
End With
'(6a)Add another sub control give it a Caption and tell it which macro to run (OnAction)
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Delete Tables"
.OnAction = "deletealltables"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "About"
.OnAction = "OpenAboutUs"
End With
On Error GoTo 0
End Sub
"Compile Error:
User-defined type not defined"
Private Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
'(1)Delete any existing one.We must useOn Error Resume Next in case it does not exist.
On Error Resume Next
Application.CommandBars("Menu Bar").Controls("Import Tables").Delete
'(2)Set a CommandBar variable to Worksheet menu bar
Set cbMainMenuBar = Application.CommandBars("Menu Bar")
'(3)Return the Index number of the Help menu. We can then use this to place a custom menu before.
iHelpMenu = cbMainMenuBar.Controls("Help").Index
'(4)Add a Control to the "Worksheet Menu Bar" before Help
'Set a CommandBarControl variable to it
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu)
With cbcCutomMenu
.Caption = "Import Tables"
End With
'(5)Give the control a caption cbcCutomMenu.Caption = "&New Menu"
'(6)Working with our new Control, add a sub control and give it a Caption and tell it which macro to run (OnAction).
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Import Tables"
.OnAction = "ImportTables"
End With
'(6a)Add another sub control give it a Caption and tell it which macro to run (OnAction)
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Delete Tables"
.OnAction = "deletealltables"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "About"
.OnAction = "OpenAboutUs"
End With
On Error GoTo 0
End Sub