Word 2003 has wrong active window after replace all

M

murdoch

Hi

Has anyone a fix for this one please? It looks like a bug
to me.

I have 2 docs open in print layout view and run the code
below in the first doc. It runs OK. I then change to the
second doc and run the code again. After the "replace
all", Word has set the activewindow back to the first doc
and after entering the header, the activedocument has also
been reset to the first doc. The user is still in the
second doc but it is not properly active, ie no flashing
cursor and typing has no effect on either doc until the
user explicitly selects one. Also only the first doc
becomes dirty.

This is using Word 2003 (11.6113.5703) on WinXP Pro SP1 -
it does not happen in previous versions (2002, 2000, 97).

Many thanks for any help

' Test code:
Public Sub main()

Debug.Print
Debug.Print "---------------"
Debug.Print "start"
Debug.Print "Active Doc is " & ActiveDocument.Name
Debug.Print "Active Window is " & ActiveWindow.Caption
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.HomeKey unit:=wdStory
With Selection.Find
.Text = "aaa" ' doesn't matter if this text
exists
.Replacement.Text = "bbb"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Debug.Print "---------------"
Debug.Print "after replace all"
' Active Doc still OK here ...
Debug.Print "Active Doc is " & ActiveDocument.Name
' ... but Active Window is now wrong
Debug.Print "Active Window is " & ActiveWindow.Caption
' next line errors if there is no such header (as
expected)
ActiveWindow.ActivePane.View.SeekView =
wdSeekPrimaryHeader
Debug.Print "---------------"
Debug.Print "after entering header"
' now Active Doc is wrong too
Debug.Print "Active Doc is " & ActiveDocument.Name
' and Active Window is still wrong
Debug.Print "Active Window is " & ActiveWindow.Caption
ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument

End Sub
 
J

JB

Hi,
Best way to do this would be to name your documents and check the active
window before making any changes...a bit of error checking wouldn't go
amiss!

Have a look at this...should give you an idea or 2. Not exactly what you
are looking for but it should set a spark off.

Dim iThisDocWinNum As Integer
Dim myWin As Window
Dim fOtherDocOpen as Boolean

iThisDocWinNum = ActiveWindow.Index

fOtherDocOpen = False
If Application.Windows.Count > 1 Then
For Each myWin In Application.Windows
If (InStr(myWin.Document.Name, "Document1") Or _
InStr(myWin.Document.Name, "Document2") And _
myWin.Index <> iThisDocWinNum Then
fOtherDocOpen = True
End If
Next
End If


HTH

J
 
M

murdoch

Hi

Thanks for the response. I think I might prefer to do
something like setting my own variables to activedocument
and activedocument.activewindow and ALWAYS working with
those as in MyWindow.selection... etc which does seem to
work, at least with the code I posted (which was hacked
out of a much larger module). Given we often do SaveAs and
close and re-open docs I think this will be easier.

What really worries me is that if something as basic as
the activewindow is now unreliable (compared with Word 97,
2000 and 2002), what else is now also broken in VBA? Never
mind the cost of going through literally thousands of
lines of code developed over 6 years to program round it.
(I feel a rant coming on!)
 
J

JB

LMAO I've had endless problems with versions above 2000 as well and am now
seriousley considering developing for Open Office in Basic. It's a bit
rougher round the edges but it'll do the job without as much chopping and
changing!

Another thing to think about is when micro$oft no longer support plain old
VBA in favout of .NET and all our macros written in VBA are useless!

BASTARDS! :)

J
 
G

Greg Oij

Murdoch:
I have EXACTLY the same problem! I have an application
that pops up an unsaved Word Doc and must be compatible
with Word '97, Word 2000, Word 2002, and now 2003. My
most recent post (no responses)...
___________________________
I have an application that displays a document on top of
another one after analyzing the origional one.

It is compatible with Word '97, Word 2000, Word XP, and,
with one small exception, Word 2003.

When the new document is displayed, it should be
displayed on top of the origional document. The new
document is always displayed on top of the origional in
Word '97, Word 2000, and Word XP, and about 60-80% of the
time in Word 2003.

The code that I've tried to remedy this problem is:
Windows("WindowCaptionName").Activate
Documents(DOCNUMBER).Activate
and
Documents(DOCNAME).Activate

I know that the "WindowCaptionName", DOCNUMBER, and
DOCNAME variables are correct through setting breakpoints.

When that didn't work 100% of the time (again, this
problem only occurs in Word 2003), I tried:

Declare Function GetActiveWindow Lib "user32" () as Long
Declare Function SetForegroundWindow Lib "user32" (ByVal
hwnd As Long) as Long
Dim lRetVal as Long, lHandle as Long

'Here, correct window is active but not always on top
lHandle = GetActiveWindow()

lRetVal = SetForegroundWindow(lHandle)

after, which didn't seem to make any difference.

Is there a VBA routine that will guarantee that the new
document is always displayed on top of the origional doc?

Thank you for any ideas that you might have.
 
W

Word Heretic

G'day "Greg Oij" <[email protected]>,

Try wiggling the cursor in the new document

Selection.MoveLeft
Selection.MoveRight


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Greg Oij reckoned:
 
G

Greg Oij

I think this is a bug in Word 2003 also...
Steve:
Good idea! The sought doc now comes up more frequently...
The wrong doc does come up occasionally.
Thanks!
(there has to be a way to ABSOLUTELY make one doc come up
on top!!!!.... I just can't find it.)
 
W

Word Heretic

G'day "Greg Oij" <[email protected]>,

Be more dramatic with your wiggling I guess!!!
Maybe wiggle a wdStory's worth rather than a char


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Greg Oij reckoned:
 
J

JB

LMAO Steve @ "Be more dramatic with your wiggling".

Anyway, would it not be better if you could saveas when you create the
document and give a decent name then initialize the document based on name?

J
 
W

Word Heretic

G'day JB <[email protected]>,

Doesn't help the 'view here' unfortunately - its a Word thing :-(

Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


JB reckoned:
 

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