Access to Word

L

Lester Lane

This is very odd. 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... Sometimes it will run again - maybe after a compile or
something. Am trying to find out what will trigger it. 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
......
 
P

Paul Shapiro

Thanks Tom. I've had that issue sometimes in the past and it never occurred
to me that code could compile ok but only because it was creating a hidden
reference to a Word object which then becomes invalid. Nice to know. I would
rather have the compiler flag that kind of code as an error- it would be a
lot easier to find. But I guess we don't get that choice.

Tom Wickerath said:
Hi Lester,

See if this KB article is helpful:
http://support.microsoft.com/kb/189618


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________

Lester Lane said:
This is very odd. 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... Sometimes it will run again - maybe after a compile or
something. Am trying to find out what will trigger it. 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

Thanks Tom. I've had that issue sometimes in the past and it never occurred
to me that code could compile ok but only because it was creating a hidden
reference to a Word object which then becomes invalid. Nice to know. I would
rather have the compiler flag that kind of code as an error- it would be a
lot easier to find. But I guess we don't get that choice.

Tom Wickerath said:
Hi Lester,
See if this KB article is helpful:
   http://support.microsoft.com/kb/189618
Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
This is very odd. 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...  Sometimes it will run again - maybe after a compile or
something.  Am trying to find out what will trigger it.  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

It certainly seems that this is the problem. I added objWordApp in
front to the Dialogs(wdDialogFileNew) but still no joy. I'm not that
experienced in this. Are there other parts of the code that are
setting up this hidden global variable? Thanks
 
P

Paul Shapiro

Lester Lane said:
Thanks Tom. I've had that issue sometimes in the past and it never
occurred
to me that code could compile ok but only because it was creating a
hidden
reference to a Word object which then becomes invalid. Nice to know. I
would
rather have the compiler flag that kind of code as an error- it would be
a
lot easier to find. But I guess we don't get that choice.

Hi Lester,
See if this KB article is helpful:
http://support.microsoft.com/kb/189618
Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
"Lester Lane" wrote:
This is very odd. 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... Sometimes it will run again - maybe after a compile or
something. Am trying to find out what will trigger it. 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

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

It certainly seems that this is the problem. I added objWordApp in
front to the Dialogs(wdDialogFileNew) but still no joy. I'm not that
experienced in this. Are there other parts of the code that are
setting up this hidden global variable? Thanks

I haven't tried this, but since your sample code shows the Word application
dimensioned as Object rather than Word.Application, maybe you could
temporarily remove the VBA reference to Word? That should make any implicit
Word objects generate a compile error. If you do have any other Word object
legitimately dimensioned as a specific Word object rather than generic
objects, that would also generate a compile error.
 
L

Lester Lane

Thanks Tom. I've had that issue sometimes in the past and it never
occurred
to me that code could compile ok but only because it was creating a
hidden
reference to a Word object which then becomes invalid. Nice to know. I
would
rather have the compiler flag that kind of code as an error- it would be
a
lot easier to find. But I guess we don't get that choice.
"Tom Wickerath" <AOS168b AT comcast DOT net> wrote in
message
Hi Lester,
See if this KB article is helpful:
   http://support.microsoft.com/kb/189618
Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
:
This is very odd. The first time through is fine.  If I leave
Word running then the next run is good too.  If I close Word thenthe
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...  Sometimes it will run again - maybe after a compile or
something.  Am trying to find out what will trigger it.  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
It certainly seems that this is the problem.  I added objWordApp in
front to the Dialogs(wdDialogFileNew) but still no joy.  I'm not that
experienced in this.  Are there other parts of the code that are
setting up this hidden global variable?  Thanks

I haven't tried this, but since your sample code shows the Word application
dimensioned as Object rather than Word.Application, maybe you could
temporarily remove the VBA reference to Word? That should make any implicit
Word objects generate a compile error. If you do have any other Word object
legitimately dimensioned as a specific Word object rather than generic
objects, that would also generate a compile error.

Yes, seems to be a bit of both. Word objects not specifically defined
as Word objects and then not fully referenced. 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
......

I also later had referenced ActivceDocument without the objWordApp.
first. This now sorted - many thanks to all.
 

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