Which menu item activated - how?

M

Mike Boson

Hi

I'd like to be able to identify if a menu item (any menu item) has been
activated both by click and by accelerator key. I found a message on google
groups that kind of provides a solution but I'm having trouble figuring it
out and I was wondering if anyone might be able to explain how to use it?

The text of the post is below. I understand steps 1-3 but I get lost at
step 4:

How would I call the sub in step 4? Would I just put it in a regular
module? What would be an example of an "Ctrl" argument for the sub in step 4?

Also, I note that the sub is called "MyControl_Click". Would that work even
if the user activated the menu item using an accelerator key, not the mouse?

The post says that code needs to be written to instantiate the class object.
Does that mean a Property Get statement?

Lastly, in step 3 the SaveAs button ID is specified. How would that Set
statement be written if I wanted to get the id number for whatever menu item
was activated?

Many thanks for any help! Here's the original post:

"You can do it by trapping the click event for the control.

1. Add a class module to the VBA project.

2. Declare a variable to trap the events:

Public WithEvents MyControl As Office.CommandBarButton

3. In the module's initialize event, find the control you want to monitor.

Private Sub Class_Initialize()
Set MyControl = Application.CommandBars.FindCo­ntrol(ID:=748)
End Sub

(748 is the ID of SaveAs option on the File menu).

4. Write an event procedure for when the control is clicked:

Private Sub MyControl _Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
MsgBox "FileSave Clicked"
End Sub

The click event is fired before the built-in command is run. Set
CancelDefault = TRUE to prevent the built-in command running.

5. Write some code (eg in AutoExec) to instantiate your class object. You'll
need to re-run this code if you do anything that resets the VBA project,
like editing any code."

(The original post I'm quoting from is
http://groups-beta.google.com/group/microsoft.public.word.vba.general/msg/5c81f19b8c21b4b5?hl=en&)

Thanks again
Mike
 
J

Jezebel

You don't *call* the event procedure shown at step 4. Event procedures are
called automatically when the given even happens. You might be more familiar
with the event procedures in a UserForm (UserForm are special kinds of class
modules, and -- in this respect -- work in pretty much the same way.) If you
have a TextBox on your UserForm, you can write an event procedure --
textbox1_Click() -- which is called automatically when the textbox's click
event happens. Similarly, the Word Application object and the Document
object have trappable events (have a look at the object browser -- events
are the functions with a little lightning bolt icon.

And, most salient in this case, command bar objects have click events, which
you can trap: the click event is fired when the user clicks the control: and
the toolbars mechanism supplies the Ctrl argument.
 
M

Mike Boson

Thanks but sorry, I'm as confused as I was at the beginning.

I am familiar with event procedures, but I can't figure out how to use the
procedure in step 4 (let alone instantiating the class properly). I've tried
putting it in a module but nothing happens. The event procedure has
arguments that need to be supplied - so how do they get supplied?

There were a bunch of other questions I asked in my post that I'm sure
affect whether I can get the code to work...

Any guidance or enlightenment from anyone would be *greatly* appreciated.

TIA
Mike
 
J

Jezebel

1) The event procedure must be in a *class* module, not an ordinary module.


2) To instantiate the class: create an ordinary module with code along these
lines ---

Private mClassVar as clsMyClass 'Module-level
variable, 'clsMyClass' is whatever you called your class module

'Use AutoExec if you want the class to instantiate automatically when the
template loads
Public Sub AutoExec()
Set mClassVar = new clsMyClass 'Instantiate the
class. Note that the class Initialize function runs at this point
End Sub


3) The arguments to the event procedure are provided automatically by the
event itself. You don't have to do anything about these.
 
M

Mike Boson

That's great, now I get it! Another question if you don't mind?

How should I identify a control that doesn't have a ID number? I modified
the code below (from MS KB) to return the ID numbers of all controls, but:

Some menu controls don't turn up in the results (for instance File>Close).
Are those just not available or is there another way to trap them?

Other controls that have been added by add-ins or manually all have an ID of
"1". Is the only way to identify those customised menu items by using their
Tag?

Thanks for any help...
Mike

Sub FindFaceIDsWordDocument()

'This code prints the information to a new Word document.

Dim c As CommandBar, ctl As CommandBarControl

Application.Documents.Add

For Each c In Application.CommandBars
For Each ctl In c.Controls
Selection.TypeText "CommandBar Name: " & c.Name & _
" | Caption: " & ctl.Caption & " | ID: " & ctl.ID
Selection.TypeParagraph
Next
Next

End Sub

RESULTS>>> (edited)
CommandBar Name: Standard | Caption: New &Blank Document | ID: 2520
CommandBar Name: Standard | Caption: &Open... | ID: 23
CommandBar Name: Standard | Caption: &Save | ID: 3
CommandBar Name: Standard | Caption: &Mail Recipient | ID: 3738
CommandBar Name: Standard | Caption: &Print | ID: 2521
CommandBar Name: Standard | Caption: Print Pre&view | ID: 109
CommandBar Name: Standard | Caption: Print to PaperPort (PDF) | ID: 1
CommandBar Name: Standard | Caption: &Spelling and Grammar... | ID: 2566
CommandBar Name: Standard | Caption: Cu&t | ID: 21
CommandBar Name: Standard | Caption: &Copy | ID: 19
CommandBar Name: Standard | Caption: &Paste | ID: 22
CommandBar Name: Standard | Caption: &Format Painter | ID: 108
CommandBar Name: Standard | Caption: &Undo VBA-Selection.TypeParagraph | ID:
128
CommandBar Name: Standard | Caption: Can't &Redo | ID: 129
CommandBar Name: Standard | Caption: Hyperl&ink... | ID: 1576
CommandBar Name: Standard | Caption: &Tables and Borders Toolbar | ID: 916
CommandBar Name: Standard | Caption: &Insert Table... | ID: 333
CommandBar Name: Standard | Caption: &Insert Excel Spreadsheet | ID: 142
CommandBar Name: Standard | Caption: &Columns... | ID: 9
CommandBar Name: Standard | Caption: &Drawing | ID: 204
CommandBar Name: Standard | Caption: &Document Map | ID: 1714
CommandBar Name: Standard | Caption: &Show All | ID: 119
CommandBar Name: Standard | Caption: &Zoom: | ID: 1733
CommandBar Name: Standard | Caption: Microsoft Word &Help | ID: 984
CommandBar Name: Standard | Caption: Insert Contact | ID: 1
[etc]
 
J

Jezebel

A simpler approach if you're adding you're on controls is to specify the
function that is called when they run (as opposed to using an event). Then
you can use ActiveControl to identify the actual control that was activated,
and check its tag. There's an additional property (the name of which escapes
at the moment) you can use if you need a second piece of information, eg if
you have a control array.

Also, can't you set the ID value of the controls you add? (Can't remember
that off-hand either...)







Mike Boson said:
That's great, now I get it! Another question if you don't mind?

How should I identify a control that doesn't have a ID number? I modified
the code below (from MS KB) to return the ID numbers of all controls, but:

Some menu controls don't turn up in the results (for instance File>Close).
Are those just not available or is there another way to trap them?

Other controls that have been added by add-ins or manually all have an ID
of
"1". Is the only way to identify those customised menu items by using
their
Tag?

Thanks for any help...
Mike

Sub FindFaceIDsWordDocument()

'This code prints the information to a new Word document.

Dim c As CommandBar, ctl As CommandBarControl

Application.Documents.Add

For Each c In Application.CommandBars
For Each ctl In c.Controls
Selection.TypeText "CommandBar Name: " & c.Name & _
" | Caption: " & ctl.Caption & " | ID: " & ctl.ID
Selection.TypeParagraph
Next
Next

End Sub

RESULTS>>> (edited)
CommandBar Name: Standard | Caption: New &Blank Document | ID: 2520
CommandBar Name: Standard | Caption: &Open... | ID: 23
CommandBar Name: Standard | Caption: &Save | ID: 3
CommandBar Name: Standard | Caption: &Mail Recipient | ID: 3738
CommandBar Name: Standard | Caption: &Print | ID: 2521
CommandBar Name: Standard | Caption: Print Pre&view | ID: 109
CommandBar Name: Standard | Caption: Print to PaperPort (PDF) | ID: 1
CommandBar Name: Standard | Caption: &Spelling and Grammar... | ID: 2566
CommandBar Name: Standard | Caption: Cu&t | ID: 21
CommandBar Name: Standard | Caption: &Copy | ID: 19
CommandBar Name: Standard | Caption: &Paste | ID: 22
CommandBar Name: Standard | Caption: &Format Painter | ID: 108
CommandBar Name: Standard | Caption: &Undo VBA-Selection.TypeParagraph |
ID:
128
CommandBar Name: Standard | Caption: Can't &Redo | ID: 129
CommandBar Name: Standard | Caption: Hyperl&ink... | ID: 1576
CommandBar Name: Standard | Caption: &Tables and Borders Toolbar | ID: 916
CommandBar Name: Standard | Caption: &Insert Table... | ID: 333
CommandBar Name: Standard | Caption: &Insert Excel Spreadsheet | ID: 142
CommandBar Name: Standard | Caption: &Columns... | ID: 9
CommandBar Name: Standard | Caption: &Drawing | ID: 204
CommandBar Name: Standard | Caption: &Document Map | ID: 1714
CommandBar Name: Standard | Caption: &Show All | ID: 119
CommandBar Name: Standard | Caption: &Zoom: | ID: 1733
CommandBar Name: Standard | Caption: Microsoft Word &Help | ID: 984
CommandBar Name: Standard | Caption: Insert Contact | ID: 1
[etc]
 
M

Mike Boson

Thanks for the idea about ActiveControl. The problem I have with controls
that have ID=1 is that a lot of them are in third party add-ins and I don't
have any control over their IDs. Those third party controls don't have Tags
either. So the only way to identify them would be ActiveControl I guess...

Thanks again.

Jezebel said:
A simpler approach if you're adding you're on controls is to specify the
function that is called when they run (as opposed to using an event). Then
you can use ActiveControl to identify the actual control that was activated,
and check its tag. There's an additional property (the name of which escapes
at the moment) you can use if you need a second piece of information, eg if
you have a control array.

Also, can't you set the ID value of the controls you add? (Can't remember
that off-hand either...)







Mike Boson said:
That's great, now I get it! Another question if you don't mind?

How should I identify a control that doesn't have a ID number? I modified
the code below (from MS KB) to return the ID numbers of all controls, but:

Some menu controls don't turn up in the results (for instance File>Close).
Are those just not available or is there another way to trap them?

Other controls that have been added by add-ins or manually all have an ID
of
"1". Is the only way to identify those customised menu items by using
their
Tag?

Thanks for any help...
Mike

Sub FindFaceIDsWordDocument()

'This code prints the information to a new Word document.

Dim c As CommandBar, ctl As CommandBarControl

Application.Documents.Add

For Each c In Application.CommandBars
For Each ctl In c.Controls
Selection.TypeText "CommandBar Name: " & c.Name & _
" | Caption: " & ctl.Caption & " | ID: " & ctl.ID
Selection.TypeParagraph
Next
Next

End Sub

RESULTS>>> (edited)
CommandBar Name: Standard | Caption: New &Blank Document | ID: 2520
CommandBar Name: Standard | Caption: &Open... | ID: 23
CommandBar Name: Standard | Caption: &Save | ID: 3
CommandBar Name: Standard | Caption: &Mail Recipient | ID: 3738
CommandBar Name: Standard | Caption: &Print | ID: 2521
CommandBar Name: Standard | Caption: Print Pre&view | ID: 109
CommandBar Name: Standard | Caption: Print to PaperPort (PDF) | ID: 1
CommandBar Name: Standard | Caption: &Spelling and Grammar... | ID: 2566
CommandBar Name: Standard | Caption: Cu&t | ID: 21
CommandBar Name: Standard | Caption: &Copy | ID: 19
CommandBar Name: Standard | Caption: &Paste | ID: 22
CommandBar Name: Standard | Caption: &Format Painter | ID: 108
CommandBar Name: Standard | Caption: &Undo VBA-Selection.TypeParagraph |
ID:
128
CommandBar Name: Standard | Caption: Can't &Redo | ID: 129
CommandBar Name: Standard | Caption: Hyperl&ink... | ID: 1576
CommandBar Name: Standard | Caption: &Tables and Borders Toolbar | ID: 916
CommandBar Name: Standard | Caption: &Insert Table... | ID: 333
CommandBar Name: Standard | Caption: &Insert Excel Spreadsheet | ID: 142
CommandBar Name: Standard | Caption: &Columns... | ID: 9
CommandBar Name: Standard | Caption: &Drawing | ID: 204
CommandBar Name: Standard | Caption: &Document Map | ID: 1714
CommandBar Name: Standard | Caption: &Show All | ID: 119
CommandBar Name: Standard | Caption: &Zoom: | ID: 1733
CommandBar Name: Standard | Caption: Microsoft Word &Help | ID: 984
CommandBar Name: Standard | Caption: Insert Contact | ID: 1
[etc]
 
J

Jezebel

As long as you tag your own controls, then you can safely ignore the tagless
ones?




Mike Boson said:
Thanks for the idea about ActiveControl. The problem I have with controls
that have ID=1 is that a lot of them are in third party add-ins and I
don't
have any control over their IDs. Those third party controls don't have
Tags
either. So the only way to identify them would be ActiveControl I
guess...

Thanks again.

Jezebel said:
A simpler approach if you're adding you're on controls is to specify the
function that is called when they run (as opposed to using an event).
Then
you can use ActiveControl to identify the actual control that was
activated,
and check its tag. There's an additional property (the name of which
escapes
at the moment) you can use if you need a second piece of information, eg
if
you have a control array.

Also, can't you set the ID value of the controls you add? (Can't remember
that off-hand either...)







Mike Boson said:
That's great, now I get it! Another question if you don't mind?

How should I identify a control that doesn't have a ID number? I
modified
the code below (from MS KB) to return the ID numbers of all controls,
but:

Some menu controls don't turn up in the results (for instance
File>Close).
Are those just not available or is there another way to trap them?

Other controls that have been added by add-ins or manually all have an
ID
of
"1". Is the only way to identify those customised menu items by using
their
Tag?

Thanks for any help...
Mike

Sub FindFaceIDsWordDocument()

'This code prints the information to a new Word document.

Dim c As CommandBar, ctl As CommandBarControl

Application.Documents.Add

For Each c In Application.CommandBars
For Each ctl In c.Controls
Selection.TypeText "CommandBar Name: " & c.Name & _
" | Caption: " & ctl.Caption & " | ID: " & ctl.ID
Selection.TypeParagraph
Next
Next

End Sub

RESULTS>>> (edited)
CommandBar Name: Standard | Caption: New &Blank Document | ID: 2520
CommandBar Name: Standard | Caption: &Open... | ID: 23
CommandBar Name: Standard | Caption: &Save | ID: 3
CommandBar Name: Standard | Caption: &Mail Recipient | ID: 3738
CommandBar Name: Standard | Caption: &Print | ID: 2521
CommandBar Name: Standard | Caption: Print Pre&view | ID: 109
CommandBar Name: Standard | Caption: Print to PaperPort (PDF) | ID: 1
CommandBar Name: Standard | Caption: &Spelling and Grammar... | ID:
2566
CommandBar Name: Standard | Caption: Cu&t | ID: 21
CommandBar Name: Standard | Caption: &Copy | ID: 19
CommandBar Name: Standard | Caption: &Paste | ID: 22
CommandBar Name: Standard | Caption: &Format Painter | ID: 108
CommandBar Name: Standard | Caption: &Undo VBA-Selection.TypeParagraph
|
ID:
128
CommandBar Name: Standard | Caption: Can't &Redo | ID: 129
CommandBar Name: Standard | Caption: Hyperl&ink... | ID: 1576
CommandBar Name: Standard | Caption: &Tables and Borders Toolbar | ID:
916
CommandBar Name: Standard | Caption: &Insert Table... | ID: 333
CommandBar Name: Standard | Caption: &Insert Excel Spreadsheet | ID:
142
CommandBar Name: Standard | Caption: &Columns... | ID: 9
CommandBar Name: Standard | Caption: &Drawing | ID: 204
CommandBar Name: Standard | Caption: &Document Map | ID: 1714
CommandBar Name: Standard | Caption: &Show All | ID: 119
CommandBar Name: Standard | Caption: &Zoom: | ID: 1733
CommandBar Name: Standard | Caption: Microsoft Word &Help | ID: 984
CommandBar Name: Standard | Caption: Insert Contact | ID: 1
[etc]
 
M

Mike Boson

I wish I could ignore the tagless ones but some of them are the ones I need
to catch. Oh well. Guess I'll have to get to the developers and ask them
what the macro names are for the relevant controls so I can use
ActiveControl. And maybe ask them to Tag or ID their controls in the future.

Thanks again for your help.
Mike

Jezebel said:
As long as you tag your own controls, then you can safely ignore the tagless
ones?




Mike Boson said:
Thanks for the idea about ActiveControl. The problem I have with controls
that have ID=1 is that a lot of them are in third party add-ins and I
don't
have any control over their IDs. Those third party controls don't have
Tags
either. So the only way to identify them would be ActiveControl I
guess...

Thanks again.

Jezebel said:
A simpler approach if you're adding you're on controls is to specify the
function that is called when they run (as opposed to using an event).
Then
you can use ActiveControl to identify the actual control that was
activated,
and check its tag. There's an additional property (the name of which
escapes
at the moment) you can use if you need a second piece of information, eg
if
you have a control array.

Also, can't you set the ID value of the controls you add? (Can't remember
that off-hand either...)







That's great, now I get it! Another question if you don't mind?

How should I identify a control that doesn't have a ID number? I
modified
the code below (from MS KB) to return the ID numbers of all controls,
but:

Some menu controls don't turn up in the results (for instance
File>Close).
Are those just not available or is there another way to trap them?

Other controls that have been added by add-ins or manually all have an
ID
of
"1". Is the only way to identify those customised menu items by using
their
Tag?

Thanks for any help...
Mike

Sub FindFaceIDsWordDocument()

'This code prints the information to a new Word document.

Dim c As CommandBar, ctl As CommandBarControl

Application.Documents.Add

For Each c In Application.CommandBars
For Each ctl In c.Controls
Selection.TypeText "CommandBar Name: " & c.Name & _
" | Caption: " & ctl.Caption & " | ID: " & ctl.ID
Selection.TypeParagraph
Next
Next

End Sub

RESULTS>>> (edited)
CommandBar Name: Standard | Caption: New &Blank Document | ID: 2520
CommandBar Name: Standard | Caption: &Open... | ID: 23
CommandBar Name: Standard | Caption: &Save | ID: 3
CommandBar Name: Standard | Caption: &Mail Recipient | ID: 3738
CommandBar Name: Standard | Caption: &Print | ID: 2521
CommandBar Name: Standard | Caption: Print Pre&view | ID: 109
CommandBar Name: Standard | Caption: Print to PaperPort (PDF) | ID: 1
CommandBar Name: Standard | Caption: &Spelling and Grammar... | ID:
2566
CommandBar Name: Standard | Caption: Cu&t | ID: 21
CommandBar Name: Standard | Caption: &Copy | ID: 19
CommandBar Name: Standard | Caption: &Paste | ID: 22
CommandBar Name: Standard | Caption: &Format Painter | ID: 108
CommandBar Name: Standard | Caption: &Undo VBA-Selection.TypeParagraph
|
ID:
128
CommandBar Name: Standard | Caption: Can't &Redo | ID: 129
CommandBar Name: Standard | Caption: Hyperl&ink... | ID: 1576
CommandBar Name: Standard | Caption: &Tables and Borders Toolbar | ID:
916
CommandBar Name: Standard | Caption: &Insert Table... | ID: 333
CommandBar Name: Standard | Caption: &Insert Excel Spreadsheet | ID:
142
CommandBar Name: Standard | Caption: &Columns... | ID: 9
CommandBar Name: Standard | Caption: &Drawing | ID: 204
CommandBar Name: Standard | Caption: &Document Map | ID: 1714
CommandBar Name: Standard | Caption: &Show All | ID: 119
CommandBar Name: Standard | Caption: &Zoom: | ID: 1733
CommandBar Name: Standard | Caption: Microsoft Word &Help | ID: 984
CommandBar Name: Standard | Caption: Insert Contact | ID: 1
[etc]
 

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