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
 
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 routine and still no joy unless I step
through the code..

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

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