Check if Word doc is already opened

L

Lars Brownies

From within MS Access I'm trying to determine if a particular Word document
is already opened. If it is, I want to bring the document to the front,
otherwise I just want to open the document. I have the following test code:

Dim oApp As Object
Dim objDocument As Object

Set oApp = CreateObject(Class:="Word.Application")
For Each objDocument In oApp.Documents
If objDocument.Name = "MyFile.doc" Then
debug.print "Found"
End If
Next objDocument

However, this only works if I add extra code right after the:
Set oApp = CreateObject(Class:="Word.Application")
This extra code opens the Word-document.

If I open the Word document manually and then run the code, it won't notice
that the
document is already open. Is it possible to assign the already opened
documents to
oApp.Documents collection? Or is there another way to do what I want.

Thanks,

Lars
 
J

Jay Freedman

I think the problem is that you're using CreateObject rather than GetObject.
If Word isn't already running, CreateObject will start it, with no documents
in it. If Word is already running, CreateObject will start another instance
of it, which has no knowledge of what's in the Documents collection of the
first instance.

Try using GetObject instead, which just looks for a running instance. It
will need to be error-trapped with On Error Resume Next.

On Error Resume Next
Set oApp = GetObject(, "Word.Application")
If Err Then
Set oApp = New Word.Application ' or CreateObject
Set objDocument = oApp.Documents.Add("C:\foo\MyFile.doc")
Else
For Each objDocument In oApp.Documents
If objDocument.Name = "MyFile.doc" Then
' debug.print "Found"
objDocument.Activate
Exit For
End If
Next objDocument
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
L

Lars Brownies

Thanks Jay, I got it working.

Lars


Jay Freedman said:
I think the problem is that you're using CreateObject rather than
GetObject. If Word isn't already running, CreateObject will start it, with
no documents in it. If Word is already running, CreateObject will start
another instance of it, which has no knowledge of what's in the Documents
collection of the first instance.

Try using GetObject instead, which just looks for a running instance. It
will need to be error-trapped with On Error Resume Next.

On Error Resume Next
Set oApp = GetObject(, "Word.Application")
If Err Then
Set oApp = New Word.Application ' or CreateObject
Set objDocument = oApp.Documents.Add("C:\foo\MyFile.doc")
Else
For Each objDocument In oApp.Documents
If objDocument.Name = "MyFile.doc" Then
' debug.print "Found"
objDocument.Activate
Exit For
End If
Next objDocument
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
 

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