Word WindowSelectionChange does not fire

P

Petrowi

I have an app that traps several events in Word (C#). The problem is that
some of the events are fired and processed properly (WindowBeforeRightClick,
WindowBeforeDoubleClick), but WindowSelectionChange isn't firing at all.
All the event listeners are attached at the same spot in the code, all at
the same time. There aren't any other occurances of WindowSelectionChange use
in the whole solution, so I don't see how the event handler could have been
cleared.

The project is large enough that I'm missing something (~300KLoC), there are
also templates involved, but I looked at them and there is nothing about
these events in particular. The template handles toolbar clicks, menu item
clicks and DocumentChange event.

I'm out of ideas what could be preventing the SelectionChange event from
firing, any suggestions?


wap.DocumentBeforeSave += new
Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(wap_DocumentBeforeSave);
wap.WindowBeforeRightClick += new
Word.ApplicationEvents4_WindowBeforeRightClickEventHandler(wap_WindowBeforeRightClick);
wap.WindowBeforeDoubleClick += new
Word.ApplicationEvents4_WindowBeforeDoubleClickEventHandler(wap_WindowBeforeDoubleClick);
wap.WindowSelectionChange += new
Word.ApplicationEvents4_WindowSelectionChangeEventHandler(wap_WindowSelectionChange);
 
T

Tony Jollans

No reason I know why it shouldn't work (but I don't know C#), but are you
quite sure it should be firing? Is the Selection actually being changed?
Please don't take the question the wrong way; I ask because it's an often
misunderstood event that many people expect to fire when it doesn't.
 
P

Petrowi

It gets weirder.

I trimmed the whole execution down to hooking up the event handlers method.

The only other thing the method does it to run a macro:

try
{
// AppUtilities.RunMacro(wap, "Loader_Module.AutoExec"); //
<< (*)
}
catch (Exception x)
{ ... }
// now hook event handlers here...

I ran a number of test scenarios: with the template, with template, but
commented out the VBA code in AutoExec method, without the template and with
the invokation of the macro.

When the template is NOT there (file deleted) and execution of the macro is
NOT attempted (as in the code above, commented) the DocumentSelectionChange
event works just fine.

AppUtilities.RunMacro has only 2 lines of code to avoid all the ref Missing
parameters polluting the rest of the code:
object o = Type.Missing;
wordApp.Application.Run(macroName, ref o, ref o, ref o, ref
o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref
o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref
o, ref o, ref o, ref o, ref o);
 
M

Manfred F

Hi Petrowi,

:
....
I'm out of ideas what could be preventing the SelectionChange event from
firing, any suggestions?

this has been a known problem with W2002 and 2003. You should find some info
if you search for "WindowSelectionChange".
I've had the same problem in VBA, without C#. After start, the
WindowSelectionChange event works well. After some condition is reached, it
stops working.
As a workaround, You could use a timer event and generate the
WindowSelectionChange yourself.

Regards,
Manfred
 
P

Petrowi

I can't believe the culprit of this issue! One of "Microsoft's Best" kind of
problems.

DocumentSelectionChange event stops being fired if I try to run a macro that
is not there.

I assumed the presence of my Loader template is irrelevant to the problem
because there was another template that was removed from the project for
being obsolete, but calls to its macros were not properly removed. So it
didn't matter if my Loader template was there or not. After I cleaned up all
the calls to missing macros (and the exceptions were being handled, no loose
ends there) the DocumentSelectionChange event stayed attached.

Invoking missing macros did not affect WindowBeforeDoubleClick and
WindowBeforeRightClick events, which I was attaching at the same spot as
DocumentSelectionChange, which leads me to think there is a Word bug with
DocumentSelectionChange event.

I'll keep in mind the timer idea, but I really wouldn't like implementing
such a hack for it has its own potential for problems - threading, timing and
resources.
 
M

Manfred F

Hi,

:
...
DocumentSelectionChange event stops being fired if I try to run a macro that
is not there.
....

yes, that's correct. But this is not the only situation where it stops
firing - so be prepared for more problems.
I wanted to use DocumentSelectionChange in combination with a modeless
userform. The user has to select the appropriate document range, and the
userform shall react on changes. It works for some time after start, but then
it stops; I couldn't identify the circumstances, but I know, that calling a
non-existing routine is not the cause.
So for me, the timer workaround is what made things work, even if it is a
hack.

Best Regards,
Manfred
 
C

Cindy M.

Hi Petrowi (and Manfred),
I can't believe the culprit of this issue! One of "Microsoft's Best" kind of
problems.

DocumentSelectionChange event stops being fired if I try to run a macro that
is not there.
Are you sure this is the only event that stops receiving messages?

I know that doing things in the VB Editor while programming can "unhook"
events. Both problems you describe involve interaction with the VB design
interface (macro not present, non-modal Userform). So the problem makes sense,
to a certain extent.

For the Userform issue: Sometimes it helps to make sure the Userform is not
being displayed in the VBA Editor environment. Make sure it's closed.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

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