Subject: Word Automation: instance running, but can't use it?

K

kiln

I have an Access 2000 app were a user may open Word docs, the path to
the doc being stored in the db. I have this problem were if I open a
word doc via code, and the user then closes word, the next time the user
requests a word doc, it doesn't fly. An instance of Word *is* created,
you can see it in task manager (only one instance). There is no Word
instance in the period between the first and second request. What am I
doing wrong? Why doesn't the code below utilize this running instance of
Word (word 2002)?

Some of this code comes from the Access Web, I'll not include the call
that tests for a running instace of word (fIsAppRunning()), it works
fine.

The error that appears is error 462 "The remote server machine does not
exist or is unavailable". Yet, word is running. It opens, without
loading the specified doc.

Private Sub cmdOpenDoc()
On Error GoTo ErrHandler

Dim objWord As Object
Dim oDoc As Word.Document

If fIsAppRunning("Word") Then
Set objWord = GetObject(, "Word.Application")
objWord.Visible = True
Else
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
End If
' This fails if the user has opened then closed a word doc via this
code before:
Set oDoc = Word.Documents.Open(strMyDoc)

ExitHere:
Exit Sub

ErrHandler:
MsgBox "Error " & Err & ": " & Error
Resume ExitHere
End Sub
 
K

kiln

OK, someone else pointed out that I'd mixed early and late binding. To
use late binding, which is better in this case,

Set oDoc = Word.Documents.Open(strMyDoc) should become

Set oDoc = objWord.Documents.Open(strMyDoc)

That seems to have solved much.

I also needed to throw a line like so in at the end:

Set objWord = Nothing
 

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