Discussion:
WindowActivate event not fired
(too old to reply)
Bernd Schend
2009-06-14 13:52:20 UTC
Permalink
Hi all,

I've developed an Addin for Word, which needs to react to the
WindowActivate and WindowDeactivate events.

Everything works fine unless one document window is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".

Example:
Assume, there are two Word documents A and B, which both are in
"wdNormalView". Then the WindowActivate event is fired when switching
from A to B and back from B to A.

However, if A is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
then (at least with Word 2003) this happens:
a) Switching from A to B (via Strg + F6):
WindowDeactivate fired for A and WindowActivate for B
b) Switching back from B to A
WindowDeactivate fired for B (so far so good), but no WindowActivate
event for A!

Is there any deeper sense behind this behavior? In addition, is there
any way to have the WindowActivate event been fired regardless of the
Window state?

Thanks in advance for your answers.
Regards
Bernd
Bill Coan
2009-06-21 15:59:37 UTC
Permalink
Bernd,

I can confirm the behavior, but I can't explain it. It seems like a bug.
Have you checked all of the other views, to see whether they, too, fail to
trigger the event?

One of the problems with event handlers stored in a Word add-in is that they
get queued up to run simultaneously with event handlers from other add-ins.
During execution, Word switches back and forth among the event handlers,
running a line or two from each one until all the lines of code in all of
the event handlers have been executed. During this time, the
CustomizationContext can change in unpredictable ways, and in some cases the
code from one event handler can interfere with the logic of some other event
handler. Therefore, extra caution is called for. :-)

One possible way to handle this particular problem would be to detect the
view in the Deactivate event, save it for future reference, and then change
it to a view that will be certain to fire the Activate event. Then, in the
Activate event, restore the view to its original value.

In the following code (below my signature), I've used "qky" as an arbitrary
part of variable names to help make sure that my variables aren't affected
by code from some other add-in that might be running simultaneously with
mine.

Bill Coan
***@wordsite.com

Option Explicit
'reserve memory for an application variable
Private WithEvents wdApp As Word.Application
Private rng_qky As Range

Private Sub Document_New()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub Document_Open()
'assign Word to the application variable
If wdApp Is Nothing Then
Set wdApp = ThisDocument.Application
End If
End Sub

Private Sub wdApp_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
Dim strRestoreView As String
On Error Resume Next
strRestoreView = Doc.Variables("qky_RestoreView").Value
If Len(strRestoreView) > 0 Then
Doc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Not rng_qky Is Nothing Then
rng_qky.Select
End If
Doc.Variables("qky_RestoreView").Delete
Set rng_qky = Nothing
End If
'Remainder of code
MsgBox "Activate " & Doc.Name
End Sub

Private Sub wdApp_WindowDeactivate(ByVal Doc As Document, ByVal Wn As
Window)
If Doc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Then
Set rng_qky = Selection.Range
Doc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
On Error Resume Next
Doc.Variables.Add "qky_RestoreView", "True"
Else
On Error Resume Next
Doc.Variables("qky_RestoreView").Delete
End If
'Remainder of code
MsgBox "Deactivate " & Doc.Name
End Sub
Post by Bernd Schend
Hi all,
I've developed an Addin for Word, which needs to react to the
WindowActivate and WindowDeactivate events.
Everything works fine unless one document window is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
Assume, there are two Word documents A and B, which both are in
"wdNormalView". Then the WindowActivate event is fired when switching from
A to B and back from B to A.
However, if A is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
WindowDeactivate fired for A and WindowActivate for B
b) Switching back from B to A
WindowDeactivate fired for B (so far so good), but no WindowActivate event
for A!
Is there any deeper sense behind this behavior? In addition, is there any
way to have the WindowActivate event been fired regardless of the Window
state?
Thanks in advance for your answers.
Regards
Bernd
Bernd Schend
2009-06-22 19:34:01 UTC
Permalink
Hi Bill,

thanks a lot for your analysis and for giving me more insight into
Word's event handling. Thanks in addition for the suggested workaround,
which I will implement immediately.

Kind regards
Bernd

By the way, I didn't check whether other views 'disable' the
WindowActivate event, too.
Bill Coan
2009-06-22 21:25:29 UTC
Permalink
Bernd,

You're welcome. I'm glad my ideas proved helpful. If you learn more about
the behavior of other views, be sure to post back with your findings.

Bill Coan
Post by Bernd Schend
Hi Bill,
thanks a lot for your analysis and for giving me more insight into
Word's event handling. Thanks in addition for the suggested workaround,
which I will implement immediately.
Kind regards
Bernd
By the way, I didn't check whether other views 'disable' the
WindowActivate event, too.
Tomy Varghese
2009-08-03 10:17:32 UTC
Permalink
Post by Bernd Schend
Hi all,
I've developed an Addin for Word, which needs to react to the
WindowActivate and WindowDeactivate events.
Everything works fine unless one document window is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
Assume, there are two Word documents A and B, which both are in
"wdNormalView". Then the WindowActivate event is fired when switching
from A to B and back from B to A.
However, if A is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
WindowDeactivate fired for A and WindowActivate for B
b) Switching back from B to A
WindowDeactivate fired for B (so far so good), but no WindowActivate
event for A!
Is there any deeper sense behind this behavior? In addition, is there
any way to have the WindowActivate event been fired regardless of the
Window state?
Thanks in advance for your answers.
Regards
Bernd
In some cases the DocumentChange event can be a subsitute for
WindowActivate which appears to be buggy. The WindowActivate event
strangely only in Word and not in Excel/PowerPoint!

Tomy
Tomy Varghese
2009-08-03 10:20:41 UTC
Permalink
Post by Tomy Varghese
Post by Bernd Schend
Hi all,
I've developed an Addin for Word, which needs to react to the
WindowActivate and WindowDeactivate events.
Everything works fine unless one document window is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
Assume, there are two Word documents A and B, which both are in
"wdNormalView". Then the WindowActivate event is fired when switching
from A to B and back from B to A.
However, if A is in status
"ActivePane.View.SeekView = wdSeekCurrentPageHeader".
WindowDeactivate fired for A and WindowActivate for B
b) Switching back from B to A
WindowDeactivate fired for B (so far so good), but no WindowActivate
event for A!
Is there any deeper sense behind this behavior? In addition, is there
any way to have the WindowActivate event been fired regardless of the
Window state?
Thanks in advance for your answers.
Regards
Bernd
In some cases the DocumentChange event can be a subsitute for
WindowActivate which appears to be buggy. The WindowActivate event
strangely only in Word and not in Excel/PowerPoint!
Tomy
Er .. i meant it appears to work fine in Excel / PowerPoint.

Loading...