Group:  Microsoft Access ยป microsoft.public.access.formscoding
Thread: Setting Focus to Word

Geek News

Setting Focus to Word
"Vic" <vic[ at ]showsec.com> 12/15/2008 10:30:05 AM
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

Re: Setting Focus to Word
"Damon Heron" <damon_327[ at ]hotmail.com> 12/15/2008 5:13:36 PM

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

"Vic" <vic[ at ]showsec.com> wrote in message
news:E5FD40D5-31D2-4843-99C0-25C1A883826F[ at ]microsoft.com...
[Quoted Text]
>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


Re: Setting Focus to Word
"Vic" <vic[ at ]showsec.com> 12/15/2008 5:40:27 PM
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



"Damon Heron" <damon_327[ at ]hotmail.com> wrote in message
news:e00r9htXJHA.5312[ at ]TK2MSFTNGP02.phx.gbl...
[Quoted Text]
>
> 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
>
> "Vic" <vic[ at ]showsec.com> wrote in message
> news:E5FD40D5-31D2-4843-99C0-25C1A883826F[ at ]microsoft.com...
>>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
>
>

Re: Setting Focus to Word
"Damon Heron" <damon_327[ at ]hotmail.com> 12/15/2008 9:57:05 PM
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

"Vic" <vic[ at ]showsec.com> wrote in message
news:AF3314D2-E526-4496-8331-4A7500AB3FF4[ at ]microsoft.com...
[Quoted Text]
> 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
>
>
>
> "Damon Heron" <damon_327[ at ]hotmail.com> wrote in message
> news:e00r9htXJHA.5312[ at ]TK2MSFTNGP02.phx.gbl...
>>
>> 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
>>
>> "Vic" <vic[ at ]showsec.com> wrote in message
>> news:E5FD40D5-31D2-4843-99C0-25C1A883826F[ at ]microsoft.com...
>>>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
>>
>>
>


Re: Setting Focus to Word
"Vic" <vic[ at ]showsec.com> 12/15/2008 11:25:20 PM
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



"Damon Heron" <damon_327[ at ]hotmail.com> wrote in message
news:uZ3SYAwXJHA.1188[ at ]TK2MSFTNGP05.phx.gbl...
[Quoted Text]
> 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
>
> "Vic" <vic[ at ]showsec.com> wrote in message
> news:AF3314D2-E526-4496-8331-4A7500AB3FF4[ at ]microsoft.com...
>> 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
>>
>>
>>
>> "Damon Heron" <damon_327[ at ]hotmail.com> wrote in message
>> news:e00r9htXJHA.5312[ at ]TK2MSFTNGP02.phx.gbl...
>>>
>>> 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
>>>
>>> "Vic" <vic[ at ]showsec.com> wrote in message
>>> news:E5FD40D5-31D2-4843-99C0-25C1A883826F[ at ]microsoft.com...
>>>>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
>>>
>>>
>>
>
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net