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
 
J

Jezebel

The problem is that the object you create (objWord) is not the object you
are using to open the document. Your code has a mixture of early and late
binding, and your problem is a consequence of that confusion.

Early binding means that you have added a reference to the Word library to
the project's references (which you've obviously done), and you declare your
object variables explicitly, as in Dim oDoc As Word.Document. You could
similarly use

Dim objWord as Word.Application

and instantiate it using

Set objWord = [new] Word.Application

The alternative is that you not have the project reference to Word, and
declare all the variables as Object. This is a little harder to code because
you don't get the Intellisense prompts and syntax checking; but it makes
your program independent of the version of Word the user has on their
machine.


However, your serious problem is this line: Set oDoc =
Word.Documents.Open(strMyDoc)

This opens the document with an implicit reference to the Word application;
not to objWord.

You should have: Set oDoc = objWord.Documents.Open(strMyDoc)

As a separate issue: what happens if the user already has the document open?
 
K

kiln

OK, thanks, that makes sense. I was patching together code bits and am
aware of the early/late binding issues, but am quite rusty with
automation these days.

I'll remove the early binding ref and code in this case. It made a big
difference to fix that one line!

Re: what happens if the user already has the document open? Good
question!

Also, I'm not sure how to deal with set objWord = Nothing; it doesn't
seem to shut down word if I include it at the end of that sub.

Thanks again!

The problem is that the object you create (objWord) is not the object you
are using to open the document. Your code has a mixture of early and late
binding, and your problem is a consequence of that confusion.

Early binding means that you have added a reference to the Word library to
the project's references (which you've obviously done), and you declare your
object variables explicitly, as in Dim oDoc As Word.Document. You could
similarly use

Dim objWord as Word.Application

and instantiate it using

Set objWord = [new] Word.Application

The alternative is that you not have the project reference to Word, and
declare all the variables as Object. This is a little harder to code because
you don't get the Intellisense prompts and syntax checking; but it makes
your program independent of the version of Word the user has on their
machine.


However, your serious problem is this line: Set oDoc =
Word.Documents.Open(strMyDoc)

This opens the document with an implicit reference to the Word application;
not to objWord.

You should have: Set oDoc = objWord.Documents.Open(strMyDoc)

As a separate issue: what happens if the user already has the document open?






kiln said:
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
 
J

Jezebel

set ObjWord = nothing doesn't do anything at the Word end; it only clears
your app's reference to Word. You need to quit Word first:

objWord.Quit

Be aware that there are various problems you can trigger in your code that
will prevent Word closing. Typically unhandled errors, or documents still
open (even if not visible) that Word thinks need saving.



kiln said:
OK, thanks, that makes sense. I was patching together code bits and am
aware of the early/late binding issues, but am quite rusty with
automation these days.

I'll remove the early binding ref and code in this case. It made a big
difference to fix that one line!

Re: what happens if the user already has the document open? Good
question!

Also, I'm not sure how to deal with set objWord = Nothing; it doesn't
seem to shut down word if I include it at the end of that sub.

Thanks again!

The problem is that the object you create (objWord) is not the object you
are using to open the document. Your code has a mixture of early and late
binding, and your problem is a consequence of that confusion.

Early binding means that you have added a reference to the Word library to
the project's references (which you've obviously done), and you declare your
object variables explicitly, as in Dim oDoc As Word.Document. You could
similarly use

Dim objWord as Word.Application

and instantiate it using

Set objWord = [new] Word.Application

The alternative is that you not have the project reference to Word, and
declare all the variables as Object. This is a little harder to code because
you don't get the Intellisense prompts and syntax checking; but it makes
your program independent of the version of Word the user has on their
machine.


However, your serious problem is this line: Set oDoc =
Word.Documents.Open(strMyDoc)

This opens the document with an implicit reference to the Word application;
not to objWord.

You should have: Set oDoc = objWord.Documents.Open(strMyDoc)

As a separate issue: what happens if the user already has the document open?






kiln said:
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
 

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