How to write the event handler for the custom control ( Menu Item)

L

lostwings

hi sue,

I have the following VBA code to add a "Custom Menu" to the main menu bar,
and add "Item 1" as a menu item to "Custom Menu". These two
CommandBarControls can be created correctly. But when I click the "Item 1",
it does not respond by poping out the "Hello" MsgBox.

For simplicity reason, I just omit the err event handler part to make the
snippet more clear to read. And I always assume that "Menu Bar" control is
always there before I launch this VBA code.

Thanks for any possible help.
Sub addmenu()
Dim objApp As Application
Dim colCB As CommandBars
Dim objCB As CommandBar
Dim objMenuControl As CommandBarControl
Dim objItemControl As CommandBarControl

Set objApp = CreateObject("Outlook.Application")
Set colCB = objApp.ActiveWindow.CommandBars
Set objCB = colCB("Menu Bar")

Set objMenuControl = objCB.Controls.Add(msoControlPopup)
objMenuControl.Caption = "Custom Menu"
objMenuControl.Enabled = True
objMenuControl.Visible = True

Set objItemControl = objMenuControl.Controls.Add(1)
objItemControl.Caption = "Item 1"
objItemControl.Enabled = True
objItemControl.OnAction = "myForm"
objItemControl.Visible = True


Set objApp = Nothing
Set colCB = Nothing
Set objCB = Nothing
Set objMenuControl = Nothing

End Sub

Sub myForm()
MsgBox ("hello")
End Sub
 
M

Michael Bauer

Am Thu, 17 Nov 2005 22:09:02 -0800 schrieb lostwings:

In VBA the OnAction property isn´t necessary. Instead declare your variable
objItemControl in the module head WithEvents. Then you can select
"objItemControl" in the left upper ComboBox and its "Click" event in the
right one. The IDE then adds automatically the event procedure to your code.

Sample for declaration:

Private WithEvents objItemControl as Office.CommandBarButton

Sub addmenu()
Don´t declare objItemControl here again!
....
End Sub

Private Sub objItemControl_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
This method can replace your myForm method (or call it from here)
End Sub
 

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