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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Giving focus to Word from my overlaid form

Status
Not open for further replies.

StuH

Programmer
Mar 25, 2001
53
AU
Hi,

I'm using VB6, opening an existing Word Doc by binding Later (the Word object is not referenced in the app). I have tried with Word 2000 and Word 2002. Here's what I'm trying to do.

My app opens Word and the document:

Dim WA As Object
Dim Doc As Object

Set WA = CreateObject("Word.Application")
WA.Visible = True
Set Doc = WA.Documents.Open(strDocument)

At the same time, the app minimises itself and opens a new small form set to "Always on Top" using

'Constants for topmost.
Public Const HWND_TOPMOST = -1
Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOACTIVATE = &H10
Public Const SWP_SHOWWINDOW = &H40
Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Public Declare Function SetWindowPos Lib "user32" _
_
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, _
ByVal wFlags As Long) As Long

AND

Private Sub Form_Load()

Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)

End Sub

On the form is a listbox with items added from my App. The idea is for users to type up their doc in Word, and double-click desired list items to have them inserted at the current cursor (caret) location using:

Public Function InsertField(sText As String) As Boolean
'insert selected sting after caret
WA.Selection.InsertAfter sText
'place caret at end of newly inserted string
WA.Selection.Collapse 0
'make the Word doc active
WA.Activate
End Function

The problem I am having is with the last command. Clicking the items in the list on my Form gives the form focus, and while it does insert the string into the Word doc correctly, it does not give Word focus, instead the focus remains with the form. The Word taskbar flashes though...

The idea is for the user to type into Word, double-click the desired item, have it inserted into the doc and keep typing. What happens is the focus is left with the form and extra typing does nothing.

Now here's the strange thing, it DOES work properly in the VB environment. Word loads, the form loads, focus is with Word, double-clicking a list item temporarily gives focus to the form, inserts the selected string and gives focus back to Word. It's perfect.

BUT when the app is compiled, the EXE will not give the focus to Word after my form is clicked. I have to manually click on the Word window to get the focus. That's not good.

WA.Activate doesn't seem to cut the mustard. I've also tried:

WA.Windows(1).SetFocus as was mentioned in the forum, but it only brought up an error saying it was invalid.

Can anyone tell me what I have to do to pass the focus back to Word at the end of my function?

Any help greatly appreciated.

-Stu.

 
Woohoo! I solved this one myself with a bit of API code.

I tried the SetFocus call from User32 suggested by others, but it didn't work. The one that did work was

Public Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long

But before you can use it, you must know the hWnd of the program you want to bring to the front (I was worried that my "Always on Top" form would not stay on top, but it did, Word was just given focus). I got the hWnd of Word this way, right after I opened Word and my document:

Dim FW As Long
Dim ClassName As String
Dim Res As Long

Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long


Where I opened the Word doc, I added:

FW = GetForegroundWindow 'Get handle of foreground window
ClassName = String(256, 0) 'Make a buffer for the class name
Res = GetClassName(FW, ClassName, 255) 'Get the class name
ClassName = Left(ClassName, Res) 'Remove terminating zeros

I used ClassName to verify I had Word's hWnd, which was stored in my Long variable FW. (this hWnd changes everytime you restart Word)

From there, when I wanted to insert my string value into Word from my Form's list, I replaced the WA.Activate from my original code with:

Res = BringWindowToTop(FW)

That's it! Hope it's of use. I looked through many similar threads, but none had a valid solution. I've tested this one on Win 2K with Word 2002 and on Win98 with Word 2000. It works identically on both.

Stu.






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top