Compile error

  • Thread starter peljo via AccessMonster.com
  • Start date
P

peljo via AccessMonster.com

I have copied form Internet a function that creates a toolbar on the fly.
However, when i use it i get the following error : Compile error : user
defined type not defined. Should i refer to some library ?
Here is the function :


' ----- Beginning Of Code -----
' ,This sample creates a custom toolbar with menu items (with sub menu's
within) amd a dropdown box with choices, code sample also includes the
' event andling for the items. Run CreateToolbarExample - this will create
the toolbar with menu items MenuEvents - is the event handling macro which
handles all events
' emoveMenu - Deletes the custom toolbar


Public Function CreatePopupToolbarExample()
Dim CustomToolBar As CommandBar
Dim MainCustomBar As CommandBarPopup
Dim SubMenuItem As CommandBarButton
Dim SubMenuPopUp As CommandBarPopup
Dim SubMenuPopUpItem As CommandBarButton
Dim DropDownList As CommandBarControl

Dim strToolBar As String
Dim iCount As Integer

' Replace "My Toolbar" with a name
' you want to use for your toolbar.
strToolBar = "My Toolbar"

' Create and display the Toolbar.
Set CustomToolBar = CommandBars.Add(Name:=strToolBar, _
Position:=msoBarFloating)
CustomToolBar.Visible = True

' Create Main PopUp Menu on Toolbar.
Set MainCustomBar = CustomToolBar.Controls.Add(Type:
=msoControlPopup)
MainCustomBar.Caption = "Main Menu"
' Create Dropdown list on toolbar
Set DropDownList = CustomToolBar.Controls.Add(Type:
=msoControlDropdown)

' Add a Menu Button and a Popup
With MainCustomBar.Controls
Set SubMenuItem = .Add(Type:=msoControlButton)
Set SubMenuPopUp = .Add(Type:=msoControlPopup)
End With

' Set properties for the sub button and popup menus.
With SubMenuItem
.Caption = "Sub Menu Item"
.Style = msoButtonCaption
.OnAction = "MenuEvents" ' <- Macro to run when clicked.
End With

With SubMenuPopUp
.Caption = "Sub Menu Popup"
' This optional can be used to invoke macro which toggles
' the state of child controls.
.OnAction = "MenuEvents"
End With

' Add item to Sub Menu
With SubMenuPopUp.Controls
Set SubMenuPopUpItem = .Add(Type:=msoControlButton)
End With

With SubMenuPopUpItem
.Caption = "Sub Menu Popup Item"
.OnAction = "MenuEvents" ' <- Macro to run when clicked.
End With

' Create a combo box which lists the options.

With DropDownList
.AddItem "Choice A"
.AddItem "Choice B"
.Caption = "Choices"
.TooltipText = "Select choice from the list"
.Width = 120
' default value to show
.ListIndex = 1
.OnAction = "MenuEvents"
End With

End Function

Sub MenuEvents()
'Handler for the event when user clicks on the toolbar options
Dim cmdBCtl As CommandBarControl
Set cmdBCtl = Application.CommandBars.ActionControl
' Determine the caption of the control that was clicked.
Select Case cmdBCtl.Caption

Case "Choices"
With cmdBCtl
Select Case .ListIndex
Case 1
MsgBox "You selected 1st item in dropdown list", _
vbInformation, "Menu Choice"
Case 2
MsgBox "You selected 2nd item in dropdown list", _
vbInformation, "Menu Choice"
End Select
End With
Case "Sub Menu Item"
MsgBox "You selected item:" & "Sub Menu Item", _
vbInformation, "Menu Choice"

Case "Sub Menu Popup"
' Do preprocessing, adding/deleting/
' enabling/disabling child items in the popup.
Debug.Print "Sub Menu Popup"
Case "Sub Menu Popup Item"
MsgBox "You selected item in the popup", _
vbInformation, "Menu Choice"
End Select
End Sub

Sub RemoveMenu()
On Error Resume Next
Application.CommandBars("My Toolbar").Delete
End Sub

' ----- End Of Code -----
 
R

RoyVidar

peljo via AccessMonster.com wrote in message said:
I have copied form Internet a function that creates a toolbar on the
fly. However, when i use it i get the following error : Compile error :
user defined type not defined. Should i refer to some library ?
Here is the function :


' ----- Beginning Of Code -----
' ,This sample creates a custom toolbar with menu items (with sub
menu's within) amd a dropdown box with choices, code sample also
includes the ' event andling for the items. Run CreateToolbarExample
- this will create the toolbar with menu items MenuEvents - is the
event handling macro which handles all events
' emoveMenu - Deletes the custom toolbar


Public Function CreatePopupToolbarExample()
Dim CustomToolBar As CommandBar
Dim MainCustomBar As CommandBarPopup
Dim SubMenuItem As CommandBarButton
Dim SubMenuPopUp As CommandBarPopup
Dim SubMenuPopUpItem As CommandBarButton
Dim DropDownList As CommandBarControl

Dim strToolBar As String
Dim iCount As Integer

' Replace "My Toolbar" with a name
' you want to use for your toolbar.
strToolBar = "My Toolbar"

' Create and display the Toolbar.
Set CustomToolBar = CommandBars.Add(Name:=strToolBar, _

Position:=msoBarFloating) CustomToolBar.Visible = True

' Create Main PopUp Menu on Toolbar.
Set MainCustomBar = CustomToolBar.Controls.Add(Type:
=msoControlPopup)
MainCustomBar.Caption = "Main Menu"
' Create Dropdown list on toolbar
Set DropDownList = CustomToolBar.Controls.Add(Type:
=msoControlDropdown)

' Add a Menu Button and a Popup
With MainCustomBar.Controls
Set SubMenuItem = .Add(Type:=msoControlButton)
Set SubMenuPopUp = .Add(Type:=msoControlPopup)
End With

' Set properties for the sub button and popup menus.
With SubMenuItem
.Caption = "Sub Menu Item"
.Style = msoButtonCaption
.OnAction = "MenuEvents" ' <- Macro to run when
clicked. End With

With SubMenuPopUp
.Caption = "Sub Menu Popup"
' This optional can be used to invoke macro which
toggles ' the state of child controls.
.OnAction = "MenuEvents"
End With

' Add item to Sub Menu
With SubMenuPopUp.Controls
Set SubMenuPopUpItem = .Add(Type:=msoControlButton)
End With

With SubMenuPopUpItem
.Caption = "Sub Menu Popup Item"
.OnAction = "MenuEvents" ' <- Macro to run when
clicked. End With

' Create a combo box which lists the options.

With DropDownList
.AddItem "Choice A"
.AddItem "Choice B"
.Caption = "Choices"
.TooltipText = "Select choice from the list"
.Width = 120
' default value to show
.ListIndex = 1
.OnAction = "MenuEvents"
End With

End Function

Sub MenuEvents()
'Handler for the event when user clicks on the toolbar options
Dim cmdBCtl As CommandBarControl
Set cmdBCtl = Application.CommandBars.ActionControl
' Determine the caption of the control that was clicked.
Select Case cmdBCtl.Caption

Case "Choices"
With cmdBCtl
Select Case .ListIndex
Case 1
MsgBox "You selected 1st item in dropdown
list", _ vbInformation, "Menu Choice"
Case 2
MsgBox "You selected 2nd item in dropdown
list", _ vbInformation, "Menu Choice"
End Select
End With
Case "Sub Menu Item"
MsgBox "You selected item:" & "Sub Menu Item", _
vbInformation, "Menu Choice"

Case "Sub Menu Popup"
' Do preprocessing, adding/deleting/
' enabling/disabling child items in the popup.
Debug.Print "Sub Menu Popup"
Case "Sub Menu Popup Item"
MsgBox "You selected item in the popup", _
vbInformation, "Menu Choice"
End Select
End Sub

Sub RemoveMenu()
On Error Resume Next
Application.CommandBars("My Toolbar").Delete
End Sub

' ----- End Of Code -----

It is sometimes helpful if you tell us which line it bombs on.

Try setting a reference to the Microsoft Office library.
 
D

Douglas J Steele

Keith Wilby said:
I'm guessing that this might be at least one of your problems.

No, I think Roy nailed it: CommandBar, CommandBarPopup, CommandBarButton and
so on are all objects from the Microsoft Office object library, so it's
necessary to have an appropriate reference set.
 
K

Keith Wilby

Douglas J Steele said:
No, I think Roy nailed it: CommandBar, CommandBarPopup, CommandBarButton
and
so on are all objects from the Microsoft Office object library, so it's
necessary to have an appropriate reference set.

Hi Doug, duly noted thanks, but "My Toolbar" will still have to be changed,
yes?

Keith.
 
D

Douglas J Steele

Keith Wilby said:
Hi Doug, duly noted thanks, but "My Toolbar" will still have to be changed,
yes?

Not necessarily. If you don't already have a toolbar named "My Toolbar" (and
you're content to have the toolbar named "My Toolbar"), you should be able
to leave it as is. It's simply a way of referring to the toolbar if you need
to, say, turn it on and off.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top