Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetWindowPos/GetActiveWindow with MSWord window

Status
Not open for further replies.

wekkew

Programmer
Sep 28, 1998
123
0
0
GB
Hello

I'm trying to open MSWord from a VB maximised form and ensure that the MSWord window is always on top of the maximised form. The following code doesn't work :-(
I don't get any errors, but if click on the main maximised form the MSWord window doesn't stay put.

Any suggestions much appreciated....
Kate

Private Sub Command1_Click()
Dim retval
retval = Shell("C:\Program Files\Microsoft Office\Office\winword.exe", vbNormalFocus)
If retval = 0 Then
MsgBox "error on Shell command"
else
AppHWnd= GetActiveWindow()
If AppHWnd= 0 Then
MsgBox "Error on GetActiveWindow"
end if
end if
End Sub

Private Sub Form_Click()
Dim flags As Long
Dim retval As Long
flags = &H1 Or &H20
'neither of the following lines seem to work!
'retval = BringWindowToTop(AppHWnd)
'retval = SetWindowPos(AppHWnd, 0, 0, 0, 0, 0, flags)
If retval = 0 Then MsgBox "Error"

End Sub
 
Try the following I have used succesfully

Public Const HWND_TOPMOST = -1
Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE

SetWindowPos Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
 
Try the following I have used it succesfully to force vbforms to the top, if you pass the handle of Word into 'handle' then it should work.

Public Const HWND_TOPMOST = -1
Public Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE

' Example - MakeTopMost Me.hwnd

Public Sub MakeNormal(Handle As Long)
SetWindowPos Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub
 
Dear pedlar

Thank you so much for that - you wouldn't believe how many variations of flags I'd tried - and all the time it was BRACKETS!!

I wonder if you can help with this problem (please...)

If I've used a shell statement to start MS Word, is there anyway I can check if an instance of Word is already running?

Thanks again
Kate





 
You can check if MS Word is running with this code:

Dim wrapp as Word.Application

Set wrapp = GetObject(,"Word.Application")

If TypeName(wrdApp) = "Application" Then
'There is an instance of word running
Else
'There isn't
End if

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top