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