PC Review Forums Newsgroups Microsoft Outlook Microsoft Outlook VBA Programming How to write the event handler for the custom control ( Menu Item)

Reply

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

 
Thread Tools Rate Thread
Old 18-11-2005, 07:09 AM   #1
=?Utf-8?B?bG9zdHdpbmdz?=
Guest
 
Posts: n/a
Default How to write the event handler for the custom control ( Menu Item)


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
  Reply With Quote
Old 18-11-2005, 09:00 AM   #2
Michael Bauer
Guest
 
Posts: n/a
Default Re: How to write the event handler for the custom control ( Menu Item)

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

  Reply With Quote
Old 19-11-2005, 12:24 AM   #3
=?Utf-8?B?bG9zdHdpbmdz?=
Guest
 
Posts: n/a
Default Re: How to write the event handler for the custom control ( Menu I

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

>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off