ActiveWindow broken in Word 2003?

M

Murdoch

Hi all

I previously posted a different aspect of this as "Word 2003 has wrong
active window after replace all" on 30April but it now seems this problem is
more severe than I thought. Apologies if this is too similar to be a
different subject or should have been posted on application.errors.

I now think that ActiveWindow by itself (ie not preceded by something like
"Documents(Index)." is generally unsafe in Word 2003 VBA though it wasn't in
previous versions (97 through 2002)

Are there other parts of VBA that are now also broken?

In this example ActiveWindow seems to try to pretend to be Application. Even
though there is no attempt to close the whole Word application, clicking a
button on a userform gives error 5479 "You cannot close Microsoft Office
Word because a dialog box is open. Click OK, switch to Word, and then close
the dialog box.". If I either specify the "parent" of the ActiveWindow or
use ActiveDocument instead the error does not occur. Running it from a
module with no userform involved also works but that's not what I want!

The error happens if I open an existing document, run a (modal) userform
with a button on it and click the button - the code on the button is given
below - all the info in the MsgBox is as expected (except the error of
course!) but after dismissing the MsgBox, the new doc is "in front" but is
not active (no flashing cursor) though on the task bar, it's button is the
active one. The first doc is still open.

Private Sub CommandButton1_Click()
Dim MyDoc As Document, MyWin As Window, sCap As String, msg As String,
WinNum As Long
Selection.TypeText "text in first document" & vbCr
sCap = ActiveWindow.Caption
Set MyDoc = Documents.Add
MyDoc.Activate
Selection.TypeText "Text in second document" & vbCr
Windows(sCap).Activate ' go back to first doc
On Error GoTo Handler
ActiveWindow.Close savechanges:=wdDoNotSaveChanges ' <-- gives
error
On Error GoTo 0
Unload Me
Exit Sub

Handler:
msg = "Current window: " & ActiveWindow.Caption & vbCr & vbCr & "Number
of windows open: " & _
Windows.Count & vbCr
For WinNum = 1 To Windows.Count
msg = msg & "Window " & WinNum & ": " & Windows(WinNum).Caption &
vbCr
Next WinNum
msg = msg & "Error " & Err.Number & " " & Err.Description
MsgBox msg
Unload Me
Exit Sub
End Sub
 
P

Peter Hewett

Hi Murdoch

I realise that this does not address the issues you are having with then Window object,
but why not just use a document object instead? Also I appreciate there may be more to
your problem than what you've posted - but why do you need to programmatically swap
backwards-and-forwards between documents?

When dealing with two or more documents I use Document and Range objects, it obviates many
of the problems that seem to be ensnaring you. Try this approach:

Private Sub CommandButton1_Click()
Dim docOriginal As Word.Document
Dim docNew As Word.Document
Dim rngOriginal As Word.Range
Dim rngNew As Word.Range

Set docOriginal = ActiveDocument
Set rngOriginal = ActiveDocument.Content
rngOriginal.Collapse wdCollapseEnd
rngOriginal.Text = "text in first document" & vbCr

' New document
Set docNew = Documents.Add

' Insert text at the end of the new document
Set rngNew = docNew.Range
rngNew.Collapse wdCollapseEnd
rngNew.Text = "Text in second document" & vbCr

' Now kill off the new document
docNew.Close wdDoNotSaveChanges
End Sub


Also, I can tell from your code that you are instantiating your Form incorrectly, which
wont help any in the long run!. Instantiate your Form like this:

Public Sub ShowFormTheTechnicallyCorrectWay()
Dim frmT As frmTest

Set frmT = New frmTest
Load frmT
frmT.Show
MsgBox "Form complete"
Unload frmT
Set frmT = Nothing
End Sub

Change "frmTest" in the above example to the name of your Form. In the Form itself change
any:
Unload Me

to:
Me.Hide

HTH + Cheers - Peter
 
M

Murdoch

Hi Peter

Thanks for responding. If this was new code, we certainly
wouldn't be doing it this way - especially we'd close the
doc, not the window.

My main point is that ActiveWindow is broken in Word 2003
though it works with Word 97, 2000 and 2002. Not only that
but it seems to be broken rather than have had the way it
works "corrected" or "tightened up" in some way.

Word no longer seems able to track the active window
correctly and I find this worrying - I suppose I reckon
the active window sems to be a fairly fundamental
(OK, 'basic' :) ) kind of object and so ought to work
reliably. If dear MS (I love them dearly - without them at
least half my support work wouldn't be needed!) break
things like this this late in the game, what else have
they done that I haven't discovered yet? We now need to
trawl through thousands of lines of code to see where
ActiveWindow should be replaced by ThisDoc.ActiveWindow or
ThatDoc.ActiveWindow. Previously, these things just
worked - after all, ActiveWindow "applies to" the global
and application objects as well as the document object so
syntactically it doesn't need to be qualified (if that's
the word).

The code was just to show the problem, including showing
that the correct doc gets the correct text (ie is active)
when expected and making it easy to test fixes. It's all
in one procedure so it's nice and easy for folk to copy
and paste if they wish and then run from the IDE with
minimal extra work to do if they want to run it from a
toolbar / commandbar button in Word.

The original does one or more mailmerges, optionally
inserts documents and does various other things, then
closes the original mailmerge main document to leave the
user with the heavily adjusted "form letters" document -
it would make a rather lengthy post <g>. It started off
life as WordBasic and has been developed continuously ever
since. Of course, it's not pretty by now (OK, wasn't
ever!) and has lots of kludges to get round other
VBA "features" but it does the job and we can issue a
single template for any Windows version of Word from 97 to
2002. Only now are our any of our clients moving to 2003
so now we need more kludges!

Thanks again Peter and if anyone else has comments (and
has bothered to read this far!) I'd be very interested to
see them too.


-----Original Message-----
Hi Murdoch

I realise that this does not address the issues you are
having with then Window object,
but why not just use a document object instead? Also I
appreciate there may be more to
your problem than what you've posted - but why do you need to programmatically swap
backwards-and-forwards between documents?

When dealing with two or more documents I use Document
and Range objects, it obviates many
of the problems that seem to be ensnaring you. Try this approach:

Private Sub CommandButton1_Click()
Dim docOriginal As Word.Document
Dim docNew As Word.Document
Dim rngOriginal As Word.Range
Dim rngNew As Word.Range

Set docOriginal = ActiveDocument
Set rngOriginal = ActiveDocument.Content
rngOriginal.Collapse wdCollapseEnd
rngOriginal.Text = "text in first document" & vbCr

' New document
Set docNew = Documents.Add

' Insert text at the end of the new document
Set rngNew = docNew.Range
rngNew.Collapse wdCollapseEnd
rngNew.Text = "Text in second document" & vbCr

' Now kill off the new document
docNew.Close wdDoNotSaveChanges
End Sub


Also, I can tell from your code that you are
instantiating your Form incorrectly, which
wont help any in the long run!. Instantiate your Form like this:

Public Sub ShowFormTheTechnicallyCorrectWay()
Dim frmT As frmTest

Set frmT = New frmTest
Load frmT
frmT.Show
MsgBox "Form complete"
Unload frmT
Set frmT = Nothing
End Sub

Change "frmTest" in the above example to the name of your
Form. In the Form itself change
 
P

Peter Hewett

Hi Murdoch

I haven't installed Office 2003 yet, I'm in the middle of a project for a client and won't
get around to this for at least 6 weeks. So I can't test and corroborate everything you
say about the Window object(s). But at the end of the day you have to deal with the
reality you find. If it don't work then you're going to have to adapt your code to run
with all versions of Word or create a new version for Word 2003 and beyond. If you can
mandate that users of your code have to use a specific version of Word then I guess you
can ignore the problem!

I'd bite the bullet and consider restructuring the code along the lines I suggested, this
technique will work with all version of Word.

Good luck and Cheers - Peter


Hi Peter

Thanks for responding. If this was new code, we certainly
wouldn't be doing it this way - especially we'd close the
doc, not the window.

My main point is that ActiveWindow is broken in Word 2003
though it works with Word 97, 2000 and 2002. Not only that
but it seems to be broken rather than have had the way it
works "corrected" or "tightened up" in some way.

Word no longer seems able to track the active window
correctly and I find this worrying - I suppose I reckon
the active window sems to be a fairly fundamental
(OK, 'basic' :) ) kind of object and so ought to work
reliably. If dear MS (I love them dearly - without them at
least half my support work wouldn't be needed!) break
things like this this late in the game, what else have
they done that I haven't discovered yet? We now need to
trawl through thousands of lines of code to see where
ActiveWindow should be replaced by ThisDoc.ActiveWindow or
ThatDoc.ActiveWindow. Previously, these things just
worked - after all, ActiveWindow "applies to" the global
and application objects as well as the document object so
syntactically it doesn't need to be qualified (if that's
the word).

The code was just to show the problem, including showing
that the correct doc gets the correct text (ie is active)
when expected and making it easy to test fixes. It's all
in one procedure so it's nice and easy for folk to copy
and paste if they wish and then run from the IDE with
minimal extra work to do if they want to run it from a
toolbar / commandbar button in Word.

The original does one or more mailmerges, optionally
inserts documents and does various other things, then
closes the original mailmerge main document to leave the
user with the heavily adjusted "form letters" document -
it would make a rather lengthy post <g>. It started off
life as WordBasic and has been developed continuously ever
since. Of course, it's not pretty by now (OK, wasn't
ever!) and has lots of kludges to get round other
VBA "features" but it does the job and we can issue a
single template for any Windows version of Word from 97 to
2002. Only now are our any of our clients moving to 2003
so now we need more kludges!

Thanks again Peter and if anyone else has comments (and
has bothered to read this far!) I'd be very interested to
see them too.



having with then Window object,
appreciate there may be more to
and Range objects, it obviates many
instantiating your Form incorrectly, which
Form. In the Form itself change

HTH + Cheers - Peter
 
M

Murdoch

Thanks again Peter - you're absolutely right of course. There does come a
time for old and messy code to be properly sorted out but that doesn't make
it any less painful!

We do produce just one set of code to work on any Windows version of Word
(well, 97-2003 anyway!) and, like most folk, the best we can do in client
control (sorry, service and support <g> ) is recommend they at least install
the latest patches. Thus we support most combinations from 97 on WinNT to
2003 on XP (plus we link with DBMSs sitting on Unix, Linux and Windows)

I'm glad it's not just us who are let's say cautious in taking up new
versions!
 
G

Greg Oij

Murdoch:
I concur that "document drift" and "Which document to
display on top" is a Word 2003 bug!

I have an application that analyzes compositions and
displays a new document next to (and hopefully on top
of!) the original. Word 97 - 2003. I added 'hooks' for
displaying in different versions of Word, and found that
if I add newDoc.Activate after calling ALL routines, Word
2003 gets away from "Which Document Drift" (at least I
haven't seen the problem since adding newDoc.Activate
many, many times.

Dim WordRelease as String, newDoc as Document
WordRelease = Left(Application.Version, 2)
' 8. = Word'97, 9. = Word 2000, 10 = Word XP, 11 = Word
2003

Set newDoc = Documents.Add
Then for displaying:
Select Case WordRelease
Case "11" ' Word 2003
newDoc.Activate
newDoc.ActiveWindow
Selection.MoveRight ' Twiddle the cursor on correct
doc
Selection.MoveLeft
End Select
To minimize the frequency of the wrong document being
displayed on top, I've tried MANY things...
This works MOST OF THE TIME!
Hope this helps, I'm still looking for a fix that works
ALL OF THE TIME FOR WORD 2003.
Greg
 

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