When is Word ready?

L

Lester Lane

Hi, I have the classic case of code working in step mode but not when
run. I have deliberately bound late to Word. I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##. Any help would be great as I am going round in
circles. Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word. Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject. Even tho' this returns ok the .Activate fails. Thanks in
advance.

Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer

On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset

With rsLetterData
If Not .EOF Then

'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap

If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If

On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
Do Until Err.Number = 0
Set objWordApp = GetObject(, "Word.Application")
Loop
On Error GoTo ErrTrap

## Tasks(objWordApp).Activate
With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
.......
End Function

UPDATE: Now using a wait loop but no joy unless I step through the
code..

WaitFor = 2
Start = Timer
While Timer < Start + WaitFor
DoEvents
Wend
 
F

Fumei2 via OfficeKB.com

I am not sure what ## Tasks(objWordApp).Activate is supposed to do.

It also seems in an odd order.

Lester said:
Hi, I have the classic case of code working in step mode but not when
run. I have deliberately bound late to Word. I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##. Any help would be great as I am going round in
circles. Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word. Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject. Even tho' this returns ok the .Activate fails. Thanks in
advance.

Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer

On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset

With rsLetterData
If Not .EOF Then

'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap

If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If

On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
Do Until Err.Number = 0
Set objWordApp = GetObject(, "Word.Application")
Loop
On Error GoTo ErrTrap

## Tasks(objWordApp).Activate
With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
......
End Function

UPDATE: Now using a wait loop but no joy unless I step through the
code..

WaitFor = 2
Start = Timer
While Timer < Start + WaitFor
DoEvents
Wend
 
L

Lester Lane

I am not sure what ## Tasks(objWordApp).Activate is supposed to do.

It also seems in an odd order.



Lester said:
Hi, I have the classic case of code working in step mode but not when
run.  I have deliberately bound late to Word.  I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##.  Any help would be great as I am going round in
circles.  Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word.  Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject.  Even tho' this returns ok the .Activate fails.  Thanks in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   Do Until Err.Number = 0
       Set objWordApp = GetObject(, "Word.Application")
   Loop
   On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
   WaitFor = 2
   Start = Timer
   While Timer < Start + WaitFor
       DoEvents
   Wend

The ## is showing you the line that causes the error. That line
flicks the user from Access to Word. I have found that
objWordApp.Visible = True and objWordApp.Activate make the app visible
and the latter activated but not the focus. If you know of a better
structure for this basic opening of word and making the focus please
let me know.
 
D

Doug Robbins - Word MVP

Use objWordApp.Activate instead.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Lester Lane said:
I am not sure what ## Tasks(objWordApp).Activate is supposed to do.

It also seems in an odd order.



Lester said:
Hi, I have the classic case of code working in step mode but not when
run. I have deliberately bound late to Word. I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##. Any help would be great as I am going round in
circles. Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word. Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject. Even tho' this returns ok the .Activate fails. Thanks in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap
If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
Do Until Err.Number = 0
Set objWordApp = GetObject(, "Word.Application")
Loop
On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
WaitFor = 2
Start = Timer
While Timer < Start + WaitFor
DoEvents
Wend

--
Gerry

Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1

The ## is showing you the line that causes the error. That line
flicks the user from Access to Word. I have found that
objWordApp.Visible = True and objWordApp.Activate make the app visible
and the latter activated but not the focus. If you know of a better
structure for this basic opening of word and making the focus please
let me know.
 
L

Lester Lane

Use objWordApp.Activate instead.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but not when
run.  I have deliberately bound late to Word.  I have even waitedto
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##.  Any help would be great as I am going roundin
circles.  Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word.  Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject.  Even tho' this returns ok the .Activate fails.  Thanks in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance....
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   Do Until Err.Number = 0
       Set objWordApp = GetObject(, "Word.Application")
   Loop
   On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
   WaitFor = 2
   Start = Timer
   While Timer < Start + WaitFor
       DoEvents
   Wend
The ## is showing you the line that causes the error.  That line
flicks the user from Access to Word.  I have found that
objWordApp.Visible = True and objWordApp.Activate make the app visible
and the latter activated but not the focus.  If you know of a better
structure for this basic opening of word and making the focus please
let me know.

Hi Doug - it worked first time but then not the next! Should I remove
the Tasks(objWordApp).Activate command as well?
 
D

Doug Robbins - Word MVP

Yes, I believe so.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Lester Lane said:
Use objWordApp.Activate instead.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but not when
run. I have deliberately bound late to Word. I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##. Any help would be great as I am going round
in
circles. Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word. Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject. Even tho' this returns ok the .Activate fails. Thanks in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap
If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
Do Until Err.Number = 0
Set objWordApp = GetObject(, "Word.Application")
Loop
On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
WaitFor = 2
Start = Timer
While Timer < Start + WaitFor
DoEvents
Wend
Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
The ## is showing you the line that causes the error. That line
flicks the user from Access to Word. I have found that
objWordApp.Visible = True and objWordApp.Activate make the app visible
and the latter activated but not the focus. If you know of a better
structure for this basic opening of word and making the focus please
let me know.

Hi Doug - it worked first time but then not the next! Should I remove
the Tasks(objWordApp).Activate command as well?
 
L

Lester Lane

Yes, I believe so.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


Use objWordApp.Activate instead.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but not when
run.  I have deliberately bound late to Word.  I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##.  Any help would be great as I am going round
in
circles.  Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word.  Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject.  Even tho' this returns ok the .Activate fails.  Thanks in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   Do Until Err.Number = 0
       Set objWordApp = GetObject(, "Word.Application")
   Loop
   On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
   WaitFor = 2
   Start = Timer
   While Timer < Start + WaitFor
       DoEvents
   Wend
--
Gerry
Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
The ## is showing you the line that causes the error.  That line
flicks the user from Access to Word.  I have found that
objWordApp.Visible = True and objWordApp.Activate make the app visible
and the latter activated but not the focus.  If you know of a better
structure for this basic opening of word and making the focus please
let me know.
Hi Doug - it worked first time but then not the next!  Should I remove
the Tasks(objWordApp).Activate command as well?

This is very odd, Doug. The first time through is fine. If I leave
Word running then the next run is good too. If I close Word then the
next one fails on calling the dialog for new files. 462 Error. If I
open Word first manually I still get the error. Any ideas? Code
below... Thanks

Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
Dim WaitFor As Single
Dim Start As Single

On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset

With rsLetterData
If Not .EOF Then

'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap

If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If

objWordApp.Activate

With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
......
 
L

Lester Lane

Yes, I believe so.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
news:6b773991-bbcf-40dd-ba20-23856754c497@y14g2000yqm.googlegroups.com....
On 16 Apr, 22:19, "Doug Robbins - Word MVP" <[email protected]>
wrote:
Use objWordApp.Activate instead.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but notwhen
run.  I have deliberately bound late to Word.  I have even waited to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##.  Any help would be great as I am going round
in
circles.  Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog) then
the code will run fine until I close that version of Word.  Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject.  Even tho' this returns ok the .Activate fails.  Thanks in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   Do Until Err.Number = 0
       Set objWordApp = GetObject(, "Word.Application")
   Loop
   On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
   WaitFor = 2
   Start = Timer
   While Timer < Start + WaitFor
       DoEvents
   Wend
--
Gerry
Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
The ## is showing you the line that causes the error.  That line
flicks the user from Access to Word.  I have found that
objWordApp.Visible = True and objWordApp.Activate make the app visible
and the latter activated but not the focus.  If you know of a better
structure for this basic opening of word and making the focus please
let me know.
Hi Doug - it worked first time but then not the next!  Should I remove
the Tasks(objWordApp).Activate command as well?

This is very odd, Doug. The first time through is fine.  If I leave
Word running then the next run is good too.  If I close Word then the
next one fails on calling the dialog for new files.  462 Error.  If I
open Word first manually I still get the error.  Any ideas?  Code
below...  Thanks

Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
Dim WaitFor As Single
Dim Start As Single

On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset

With rsLetterData
If Not .EOF Then

    'Is an instance of Word already open that we can bind to?
    On Error Resume Next
    Set objWordApp = GetObject(, "Word.Application")
    On Error GoTo ErrTrap

    If objWordApp Is Nothing Then
        'Word isn't already running - create a new instance...
        Set objWordApp = CreateObject("Word.Application")
        objWordApp.Visible = True
    Else
    End If

    objWordApp.Activate

    With Dialogs(wdDialogFileNew)
    If .Display = -1 Then 'clicked ok
       strTemplate = .Template
    Else
        MsgBox "No Template chosen", vbCritical, "No Template"
        Exit Function
    End If
    End With
.....

Just realised that this may be in the wrong message group as I am
trying to open word from access, maybe this should be in one of the
access groups?
 
D

Doug Robbins - Word MVP

The problem may be in the way that you are initiating the Word application.

Replace

On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap

If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If

with


On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
If Err Then
Wordwasnotrunning = True
Set objWordapp = CreateObject("Word.Application")
End If

You should declare Wordwasnotrunning as Boolean and at the end of your code,
test for its value and if True, include code to quit objWordApp and set it
to nothing.

If it is not the above, then there is a possibility that the With
Dialogs(wdDialogFileNew) is the problem. If you know the name of the
template that you want to be used, it would be better to declare a WordDoc
as an Object and then use

Set WordDoc = objWordapp.Documents.Add("TemplateName")

If there a a number of templates that could be used, it would probably be
better to use the following to get the template

Dim fd As FileDialog
Dim TemplateName as String
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.Title = "Select the Template to be used."
.InitialFileName = "%appdata%\microsoft\templates\*.dot?"
.AllowMultiSelect = False
If .Show = -1 Then
TemplatName = .SelectedItems(1)
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With

and then use

Set WordDoc = objWordapp.Documents.Add("TemplateName")


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

Lester Lane said:
Yes, I believe so.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


On 16 Apr, 22:19, "Doug Robbins - Word MVP" <[email protected]>
wrote:
Use objWordApp.Activate instead.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but not
when
run. I have deliberately bound late to Word. I have even waited
to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##. Any help would be great as I am going
round
in
circles. Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog)
then
the code will run fine until I close that version of Word. Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject. Even tho' this returns ok the .Activate fails. Thanks
in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap
If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
Do Until Err.Number = 0
Set objWordApp = GetObject(, "Word.Application")
Loop
On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
WaitFor = 2
Start = Timer
While Timer < Start + WaitFor
DoEvents
Wend
Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
The ## is showing you the line that causes the error. That line
flicks the user from Access to Word. I have found that
objWordApp.Visible = True and objWordApp.Activate make the app
visible
and the latter activated but not the focus. If you know of a better
structure for this basic opening of word and making the focus please
let me know.
Hi Doug - it worked first time but then not the next! Should I remove
the Tasks(objWordApp).Activate command as well?

This is very odd, Doug. The first time through is fine. If I leave
Word running then the next run is good too. If I close Word then the
next one fails on calling the dialog for new files. 462 Error. If I
open Word first manually I still get the error. Any ideas? Code
below... Thanks

Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
Dim WaitFor As Single
Dim Start As Single

On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset

With rsLetterData
If Not .EOF Then

'Is an instance of Word already open that we can bind to?
On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
On Error GoTo ErrTrap

If objWordApp Is Nothing Then
'Word isn't already running - create a new instance...
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
Else
End If

objWordApp.Activate

With Dialogs(wdDialogFileNew)
If .Display = -1 Then 'clicked ok
strTemplate = .Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
End With
.....
 
L

Lester Lane

The problem may be in the way that you are initiating the Word application.

Replace

On Error Resume Next
    Set objWordApp = GetObject(, "Word.Application")
    On Error GoTo ErrTrap

    If objWordApp Is Nothing Then
        'Word isn't already running - create a new instance...
        Set objWordApp = CreateObject("Word.Application")
        objWordApp.Visible = True
    Else
    End If

with

On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
If Err Then
    Wordwasnotrunning = True
    Set objWordapp = CreateObject("Word.Application")
End If

You should declare Wordwasnotrunning as Boolean and at the end of your code,
test for its value and if True, include code to quit objWordApp and set it
to nothing.

If it is not the above, then there is a possibility that the With
Dialogs(wdDialogFileNew) is the problem.  If you know the name of the
template that you want to be used, it would be better to declare a WordDoc
as an Object and then use

Set WordDoc = objWordapp.Documents.Add("TemplateName")

If there a a number of templates that could be used, it would probably be
better to use the following to get the template

Dim fd As FileDialog
Dim TemplateName as String
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
    .Title = "Select the Template to be used."
    .InitialFileName = "%appdata%\microsoft\templates\*.dot?"
    .AllowMultiSelect = False
    If .Show = -1 Then
        TemplatName = .SelectedItems(1)
    Else
        MsgBox "No Template chosen", vbCritical, "No Template"
        Exit Function
    End If
End With

and then use

Set WordDoc = objWordapp.Documents.Add("TemplateName")

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com


Yes, I believe so.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

On 16 Apr, 22:19, "Doug Robbins - Word MVP" <[email protected]>
wrote:
Use objWordApp.Activate instead.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but not
when
run.  I have deliberately bound late to Word.  I have even waited
to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##.  Any help would be great as I am going
round
in
circles.  Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog)
then
the code will run fine until I close that version of Word.  Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject.  Even tho' this returns ok the .Activate fails.  Thanks
in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   Do Until Err.Number = 0
       Set objWordApp = GetObject(, "Word.Application")
   Loop
   On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
   WaitFor = 2
   Start = Timer
   While Timer < Start + WaitFor
       DoEvents
   Wend
--
Gerry
Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
The ## is showing you the line that causes the error.  That line
flicks the user from Access to Word.  I have found that
objWordApp.Visible = True and objWordApp.Activate make the app
visible
and the latter activated but not the focus.  If you know of a better
structure for this basic opening of word and making the focus please
let me know.
Hi Doug - it worked first time but then not the next!  Should I remove
the Tasks(objWordApp).Activate command as well?
This is very odd, Doug. The first time through is fine.  If I leave
Word running then the next run is good too.  If I close Word then the
next one fails on calling the dialog for new files.  462 Error.  IfI
open Word first manually I still get the error.  Any ideas?  Code
below...  Thanks
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
Dim WaitFor As Single
Dim Start As Single
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   objWordApp.Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
.....

Thanks Doug,
I've been busy these last few days and still am! I will give it a go
v. soon. As for dumping Word if previously not running; I will miss
this bit out as I want the user to then continue and compose the
letter. I will leave it up to them if they then wish to close Word.
 
L

Lester Lane

The problem may be in the way that you are initiating the Word application.

On Error Resume Next
    Set objWordApp = GetObject(, "Word.Application")
    On Error GoTo ErrTrap
    If objWordApp Is Nothing Then
        'Word isn't already running - create a new instance...
        Set objWordApp = CreateObject("Word.Application")
        objWordApp.Visible = True
    Else
    End If

On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
If Err Then
    Wordwasnotrunning = True
    Set objWordapp = CreateObject("Word.Application")
End If
You should declare Wordwasnotrunning as Boolean and at the end of your code,
test for its value and if True, include code to quit objWordApp and setit
to nothing.
If it is not the above, then there is a possibility that the With
Dialogs(wdDialogFileNew) is the problem.  If you know the name of the
template that you want to be used, it would be better to declare a WordDoc
as an Object and then use
Set WordDoc = objWordapp.Documents.Add("TemplateName")
If there a a number of templates that could be used, it would probably be
better to use the following to get the template
Dim fd As FileDialog
Dim TemplateName as String
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
    .Title = "Select the Template to be used."
    .InitialFileName = "%appdata%\microsoft\templates\*.dot?"
    .AllowMultiSelect = False
    If .Show = -1 Then
        TemplatName = .SelectedItems(1)
    Else
        MsgBox "No Template chosen", vbCritical, "No Template"
        Exit Function
    End If
End With
and then use
Set WordDoc = objWordapp.Documents.Add("TemplateName")
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
news:d40092f0-e8c2-45b4-b063-fc98ae021fb5@c36g2000yqm.googlegroups.com....
On 18 Apr, 09:42, "Doug Robbins - Word MVP" <[email protected]>
wrote:
Yes, I believe so.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

On 16 Apr, 22:19, "Doug Robbins - Word MVP" <[email protected]>
wrote:
Use objWordApp.Activate instead.
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

I am not sure what ## Tasks(objWordApp).Activate is supposed to do.
It also seems in an odd order.
Lester Lane wrote:
Hi, I have the classic case of code working in step mode but not
when
run.  I have deliberately bound late to Word.  I have even waited
to
check Word has loaded and is ready but still I get the dreaded 462
error on line with ##.  Any help would be great as I am going
round
in
circles.  Needless to say that if Word opens ok whilst stepping
through (and I had to pause a while before loading the dialog)
then
the code will run fine until I close that version of Word.  Not
knowing how to "Wait"/"Sleep" for 2 secs in code I added the test
GetObject.  Even tho' this returns ok the .Activate fails.  Thanks
in
advance.
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bindto?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   Do Until Err.Number = 0
       Set objWordApp = GetObject(, "Word.Application")
   Loop
   On Error GoTo ErrTrap
## Tasks(objWordApp).Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
......
End Function
UPDATE: Now using a wait loop but no joy unless I step through the
code..
   WaitFor = 2
   Start = Timer
   While Timer < Start + WaitFor
       DoEvents
   Wend
--
Gerry
Message posted via
OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/word-programming/201004/1
The ## is showing you the line that causes the error.  That line
flicks the user from Access to Word.  I have found that
objWordApp.Visible = True and objWordApp.Activate make the app
visible
and the latter activated but not the focus.  If you know of abetter
structure for this basic opening of word and making the focus please
let me know.
Hi Doug - it worked first time but then not the next!  Should I remove
the Tasks(objWordApp).Activate command as well?
This is very odd, Doug. The first time through is fine.  If I leave
Word running then the next run is good too.  If I close Word then the
next one fails on calling the dialog for new files.  462 Error.  If I
open Word first manually I still get the error.  Any ideas?  Code
below...  Thanks
Dim objWordApp As Object
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer
Dim WaitFor As Single
Dim Start As Single
On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset
With rsLetterData
If Not .EOF Then
   'Is an instance of Word already open that we can bind to?
   On Error Resume Next
   Set objWordApp = GetObject(, "Word.Application")
   On Error GoTo ErrTrap
   If objWordApp Is Nothing Then
       'Word isn't already running - create a new instance...
       Set objWordApp = CreateObject("Word.Application")
       objWordApp.Visible = True
   Else
   End If
   objWordApp.Activate
   With Dialogs(wdDialogFileNew)
   If .Display = -1 Then 'clicked ok
      strTemplate = .Template
   Else
       MsgBox "No Template chosen", vbCritical, "No Template"
       Exit Function
   End If
   End With
.....

Thanks Doug,
I've been busy these last few days and still am!  I will give it a go
v. soon.  As for dumping Word if previously not running; I will miss
this bit out as I want the user to then continue and compose the
letter.  I will leave it up to them if they then wish to close Word.

Doug,
Code now...

Dim objWordApp As Word.Application
Dim diaWordTemplate As Word.Dialog
Dim rsLetterData As DAO.Recordset
Dim qdfLetterData As DAO.QueryDef
Dim strTemplate As String
Dim i As Integer
Dim intLength As Integer

On Error GoTo ErrTrap
Set qdfLetterData = db.QueryDefs("rsqryLetterData")
qdfLetterData![FacilityID] = FacilityID
Set rsLetterData = qdfLetterData.OpenRecordset

With rsLetterData
If Not .EOF Then

On Error Resume Next
Set objWordApp = GetObject(, "Word.Application")
If Err Then
Set objWordApp = CreateObject("Word.Application")
objWordApp.Visible = True
End If

objWordApp.Activate
Set diaWordTemplate = objWordApp.Dialogs(wdDialogFileNew)
If diaWordTemplate.Display = -1 Then 'clicked ok
strTemplate = diaWordTemplate.Template
Else
MsgBox "No Template chosen", vbCritical, "No Template"
Exit Function
End If
......

Thanks for your shorter bit re whether it is already running. It
seems that his error is due to not referencing Word correctly so a
hidden global variable is created, which is then divorced from the
object when Word closes. I also had further into the code
ActiveDocument commands and these are now objWordApp.ActiveDocument.
All is now well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top