Event firing several times!

B

- B@rney

Hi Guys'n Dolls!

I've been working with a Shared Office Add-In for a few weeks now.

Now one issue is bugging me. My Add-in checks, in the OpenDocument event,
whether or not my button should be visible. This is part of the business
logic, for some documents the add-in should be active and the button visible,
and for some not.

In the WindowActivate event I check a property and remove the button if the
add-in shouldn't be active for this document.

But, if I have multiple documents open, I get multiple click events from my
button!

This happens even if the other open documents don't have the button visible.

Please advice!
 
J

Jon Ebersole

I had the same problem with my Outlook project. I assume you are
accidentally firing the events twice, because you are trying to use a single
reference to handle your events for multiple documents. In my project I set
a single global object variable up with events, and whenever a new MailItem
was created (similar to word document object) I set that global variable
equal to my new object and the events fired fine.

You can see the major flaw; if more than one item was created, the previous
items events wouldn't fire, because they were no longer linked to the item.
To fix this, I thought i would call AddHandler and add my event for any newly
created window on the fly to my new item (document). If I created 5
documents, the first 4 would only fire once, but the 5 one (the last item to
be created) would fire twice. This was because I was still linking the newly
created doc to the global var.

For reference on what I discuss next...
In Outlook, an Inspector is basically any Outlook Item; email, contact,
post, appointment, etc. When i say inspector in my example, I am refering to
a MailItem (an email), which I think is basically equivalent to Word's
document object.

To fix all of my problems, I did the following...
1. Removed my global var for the MailItem (document)
2. Added a private sub to handle my button event (did not use 'handles
button.click', it is just a sub)
3. Created an inspector global object WithEvents (not sure what an inspector
equals in Word)
4. In the Inspector_New Event I call AddHandler for my new MailItem
(document) and pointed that to the sub i created to handle the button, and
made sure the AddHandler attached it to the new Inspector. This attaches the
same handler to all new MailItems (documents)
5. Added the Inspector_Activate event to the project
6. Within Activate event, it checks the Inspector type to make sure it is a
MailItem, if so, it checks to make sure it has my buttons, if not it loads
them. It then checks to make sure it has events associated with it. if not,
it adds them using AddHandler again, but of course it calls AddHandler with
the currently activated Inspector (document)

And that covers all of my bases; If they create a new MailItem or open an
existing MailItem, I am guaranteed that my events only fire once, and my
buttons get loaded.

I'm not sure if any of this applies to your problem, but good luck.
 
B

- B@rney

Thx Jon!

I've been down the same lane, but...

I haven't figured out how I can check if the event handler is allready added
(hooked up) or not. You can't do:
if(myDoc.Click != null)

Suggestions?
 
J

Jon Ebersole

Hey Barney. Well I guess you could say I cheated on my event handlers. I
didn't really have time to look into how to check to see if an event exists,
so I just re-added my event each time by doing this...

1. Within a try/catch I call RemoveHandler on that event. If it exists,
then it runs normally, if it doesn't, the catch basically lets me exit safely.
2. Then I call my AddHandler to make sure it is attached.

Nope, it isn't the most efficient method, but it got the job done quickly.
Will this method work for you?
 

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