Detect Click Event of Built in Button

S

shawnk

I have a word document with the following AutoExec macro, where i'm
attempting to detect the click event of a built in word toolbar
button. In this case the numbering icon from the E-Mail menu.


Sub AutoExec()
Set MyControl = New Class1
End Sub


Which also has the following class module named Class1


Public WithEvents MyControl As Office.CommandBarButton

Private Sub MyControl_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "Numbering Clicked"
CancelDefault = True
End Sub

Private Sub Class_Initialize()
Set MyControl = Application.CommandBars.FindControl(ID:=11)
End Sub


Can anyone help me understand what i'm doing wrong? The click event
does not trigger.

Shawn
 
G

Guest

Hi shawnk,

In your class module you're declaring MyControl as Public which I believe
makes it available to your standard code module. In the class it relates to a
CommandBarButton but in the code module it relates to the class itself.

You could declare it as private in both modules or you could use a different
variable in the code module to create an instance of the class.

In the code module I think you need to declare whichever variable you are
going to use for the class in the Declarations area as if you were to declare
it in AutoExec your class would be destroyed as soon as AutoExec finished and
the variable went out of scope.

You could try something like this:

(In the code module)
Private MyClickDetector As Class1

Sub AutoExec()
Dim MyClickDetector As Class1
Set MyClickDetector = New Class1
End Sub

(In the class module)
Private WithEvents MyControl As Office.CommandBarButton

Private Sub MyControl_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "Numbering Clicked"
CancelDefault = True
End Sub

Private Sub Class_Initialize()
Set MyControl = Application.CommandBars.FindControl(ID:=11)
End Sub

Cheers.

Ed
 

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