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
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