Setting Focus to Word

  • Thread starter Thread starter Vic
  • Start date Start date
V

Vic

I have a form that contains paths and names of word documents that when
clicked it starts word using automation and loads the document. This all
works fine but I would like the focus to stay with word after the document
is opened but focus returns to the access form when the exit sub is
encountered. Can someone please tell me how to force the focus to stay with
word? Here's the code for when a document is clicked:

Private Sub doc_Path_Click()

' Using late binding dim variables as objects
Dim objWord As Object
Dim doc As Object

Dim bolOpenedWord As Boolean
'Get pointer to Word Object

' Handle Error In-Line
On Error Resume Next

Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
'If we got an error, that means there was no Word Instance
Set objWord = CreateObject("Word.Application")
'Set Flag to let us know we opened Word
bolOpenedWord = True
End If

'Make Word Instance visible
objWord.Visible = True

'Reset Error Handler
On Error GoTo 0

Set doc = objWord.Documents.Open(Chr(34) & Me.doc_Path & Chr(34))
objWord.Activate

Set doc = Nothing
Set objWord = Nothing
Exit Sub

errHandler:
MsgBox Err.Number & ": " & Err.Description

End Sub
 
Here is my version that works fine (without all of the error checking for
simplicity):

Private Sub cmdWord_Click()
On Error GoTo Err_cmdWord_Click

Dim oApp As Object
Dim path as string
path= "c:\frmAllReports.doc"

Set oApp = CreateObject("Word.Application")
oApp.Visible = True
oApp.Documents.Open (path) 'notice I don't set the doc variable-- could
this be??
oApp.Activate
Exit_cmdWord_Click:
Exit Sub
Err_cmdWord_Click:
MsgBox Err.Description
Resume Exit_cmdWord_Click

End Sub

HTH
Damon
 
Damon,

Thanks for the reply but unfortuniately it works the exact same way as mine.
It doesn't seem to be in the code but what else could it be?


Vic
 
Vic,
I don't have this line:Set doc = objWord.Documents.Open(Chr(34) &
Me.doc_Path & Chr(34))
When I put it in, I was getting an error msg.
Are you saying that you still lose focus from the word doc when you run my
code?
Because that is not happening to me. The word doc comes up and has the
focus.

Damon
 
Damon,

Yes, I ran your exact code and only changed the document it was loading and
it still won't maintain focus. This is not a database that I created, it is
something I inherited and it appears to me this must not have anything to do
with the code but something else (what I don't know).

I've decided to use the CreateProcess and WaitForSingle functions which are
doing the job for me since I can't seem to figure out what is wrong with the
word object approach.

Thanks for the help!

Vic
 
Back
Top