wiring events to buttons

S

saavdi

Hi,
I am developing an application level COM addin for Office.
the addin has lots of buttons, and several of them appear twice in the
toolset, (once on the toolbar, once on menubar).

I have created separate class modules for each button.
The problem is that when I have multiple instances of the same button on
the toolset, clicking any one of them fires the event twice. How can I
prevent this??

here is the my code which does the linking..


Set Mycommandbars =
App.CommandBars.FindControls(Office.msoControlButton, , "MyTAG")

If Not Mycommandbars Is Nothing Then
For Each Mycommandbar In Mycommandbars
Set objItem = New clsMYClass

Set objItem.myButton = Mycommandbar
Call addToArray(objMenuItem) 'used to store the items in an
array

Next
End If


I thought of creating different tags, but does not help, as the users
would often customize(duplicate/move) the tools.

Any help appreciated!
 
K

Ken Slovak - [MVP - Outlook]

Use a unique Tag property for each button and have separate event handlers
for each button. That way each Click will only fire one event handler. The
Tag property has nothing to do with where any buttons are located or moved.
 
S

saavdi

Ken said:
Use a unique Tag property for each button and have separate event
handlers for each button. That way each Click will only fire one event
handler. The Tag property has nothing to do with where any buttons are
located or moved.

Thanks for the quick reply.
The tools would be ordinary Excel/Word icons, and users could customize
it by duplicating the icons (for eg., a user might create a custom
toolbar and add(copy) one of these icons on it).
In this case the next time the application loads, you will have 2 icons
with same Tag and hence the event will fire twice.

Is there any other option?

Thanks in advance.
 
K

Ken Slovak - [MVP - Outlook]

That seems a rather slim possibility and a bad reason not to use the only
way around the problem you described. I'm not even sure if the user copies a
custom button that it would duplicate the tag property but if it did that's
the user's problem.
 
S

Stephen Bullen

Hi Saavdi,
In this case the next time the application loads, you will have 2 icons
with same Tag and hence the event will fire twice.

No, it won't. When you set up an event hook as you have, behind the scenes
it actually hooks the class to each combination of tag and control ID. So
if you create multiple controls with the same Tag property and set up
instances of your event hook for each one, you're actually setting up
multiple event hooks for the same ID/Tag combination. In other words, it's
not the number of controls that's causing the multiple firing, but the
number of event handlers you're creating.

It's designed like that specifically so that users can create multiple
copies of your control - as all the copies share the same ID/Tag, they
will all fire the same event instance.

So there are two solutions:

1. Give all your controls the exact same ID, but only have ONE instance
of your event hook pointing to ONE of the controls (it doesn't matter
which!) - that code will be fired by all the controls.

2. Give all your controls unique Tag values and have separate instances
of your event hook for each.

Regards

Stephen Bullen
Microsoft MVP - Excel

Professional Excel Development
The most advanced Excel VBA book available
www.oaltd.co.uk/ProExcelDev
 
K

Ken Slovak - [MVP - Outlook]

And as we discovered in another context, if you set the OnAction property to
the ProgID of the addin then the ID properties won't matter.
 
S

saavdi

Ken said:
And as we discovered in another context, if you set the OnAction
property to the ProgID of the addin then the ID properties won't matter.

Thanks a lot. Really appreciate your help.
I just registered the event once and it works great.
 

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