Word 2003 Document on top

G

Greg Oij

I have an application that displays a document next to
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 80% of the
time in Word 2003.

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

I know that the "WindowCaptionName" and DOCNUMBER
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:

ActiveWindow.SetFocus

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

Is there a command in VBA for Word 2003 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.
 
H

Helmut Weber

Hi Greg,
just hints into the hopefully right direction:
Karl E. Peterson's One-Stop Source Shop
http://www.mvps.org/vb/code/ForceFore.zip
or Visual Basic Thunder
http://www.vbthunder.com/default.asp?srcget=5

plus eventually, to get the hWnd of a window
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetActiveWindow Lib "user32.dll" _
(ByVal hwnd As Long) As Long

Private Sub GetWordHandle()
Dim lHandle As Long
Dim retval As Long ' return value
' find window with the given document name
lHandle = FindWindow(vbNullString, ActiveDocument.Name & " - " _
& Application.Name)
'Set this window to the foreground
'lHandle = SetForegroundWindow(lHandle)
or
retval = SetActiveWindow(lHandle)
' set lHandle as the application's active window
'If the function succeeds,
'the return value is the handle to the window
'that was previously active.
'If the function fails, the return value is NULL.
'To get extended error information, call GetLastError.
Debug.Print retval
End Sub
Thanks to Christian Fressdorf
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
G

Greg Oij

Hemult:
Thank you very much for replying. If at all possible, I
would like to keep this in the vba .dot running in Word.

In Word VBA, I tried the declarations and
(I MUST BE MISSING SOMETHING!)

lHandle = FindWindow(params) to get the lHandle
(lHandle = 0) (RETURNS 0)
followed by
lHandle = SetForegroundWindow(lHandle)
(lHandle = 0) (RETURNS 0) (RETVAL?)

then, trying
retval = SetActiveWindow(lHandle)
(retval = 6751792 even when run multiple times?!?!)



.... doesn't change the active window...
 
H

Helmut Weber

Hi Greg,
I am very sorry. I only know, I managed it to get a window
in the foreground, that wasn't there before by VBA.
Don't ask me, how I did it. I changed this and that, without
really understanding what I did.
There must be people here, who really can do it.
Try again posting and keep on.
Greetings from Bavaria,
Helmut Weber






Gruss
Helmut Weber
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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