PDA

View Full Version : Word 2007 activex multiple LostFocus events



avi10000
08-22-2011, 03:11 PM
Hi all,

Word 2007:

I am using ActiveX textboxes on the Word document itself to control user data entry.

I have written an event handler to capture the _LostFocus() event when the user leaves a textbox.

However I have become aware that when I leave a textbox the event handler is being invoked multiple times - four, I think. That shows that the the _LostFocus() event is being fired 4 times for leaving a field just once.

Why is this happening?

And this does not seem so healthy.

TIA

-avi

Frosty
08-23-2011, 10:40 AM
Short answer: it's a bug. And it doesn't seem to exist in Word 2010.

I believe the bug also has different manifestations depending on whether you have the Home tab selected vs. other tabs.

There is no really easy work-around (Greg Maxey has been working on this issue a bit with a few other people).

In general, the concept to dealing with this kind of bug is to set a flag (bOkayToFire As Boolean) the first time you enter your _LostFocus event, so that you only really fire your code once... and then flip it to False so you can prevent the processing multiple times.

However, since I believe you will find that you get different event fires depending on which tab you have selected... you'd also need to find the code floating around which can check which ribbon you have selected before you start choosing whether to set the flag.

This is going to be a difficult process, as Greg and I just spent some time a couple of weeks ago working through it-- and both of us came to the conclusion that this is one of the many reasons why it's better to just use Word 2010 for some of this, or that we'd wait until some one paid us to really solve it ;)

Sorry to be general, but the specific solution is really going to depend on the exact needs of the project. But maybe the general information will be enough to have you focus on another way of achieving your goal, as writing around real microsoft bugs can be tedious and difficult.

geekgirlau
08-24-2011, 08:18 PM
This is one of those situations where the differences between applications becomes annoying!

Excel offers an Application.EnableEvents option that you can switch on and off to your hearts content. Unfortunately as this doesn't exist in Word a flag (or several of them) as suggested by Frosty should work.

avi10000
08-30-2011, 12:32 AM
Great thanks for the detailed and heartfelt answer.

-avraham

avi10000
08-30-2011, 04:31 AM
Btw, I found that if I put a break point in the event handler for the _LostFocus event, Word always hung. Also, twice it crashed in these circumstances and trashed all its VBA modules and forms... That was one 2:00 AM on the morning a of draft release, and after that event I almost emailed the client to say I was giving up...

-avraham