PC Review
Forums
Newsgroups
Microsoft Outlook
Microsoft Outlook VBA Programming
How to write the event handler for the custom control ( Menu Item)
Forums
Newsgroups
Microsoft Outlook
Microsoft Outlook VBA Programming
How to write the event handler for the custom control ( Menu Item)
![]() |
How to write the event handler for the custom control ( Menu Item) |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 -- John 3:16 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 -- Viele Gruesse / Best regards Michael Bauer - MVP Outlook > 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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Hi Michael,
Thanks very much for the help. It works perfectly now. The -- John 3:16 "Michael Bauer" wrote: > 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 > > -- > Viele Gruesse / Best regards > Michael Bauer - MVP Outlook > > > > > 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 > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

