Determine whether Word document is still open

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using the hyperlink proerty of a label to open a word document.

However, when I do that, the Access form is minimized. I'd like to put some
code in the timer event of the form to check to see if the document has been
closed, and if it has, then restore the form. How can I determine whether
the Word document is still open?

Dale
 
Hi Dale,

For a simple but not entirely reliable approach you could try something
like this air code:

Dim oWord As Object
Dim oDoc As Object
Dim blDocIsOpen As Boolean
Dim strDocName As String

blDocIsOpen = False
strDocName = "C:\folder\file.doc"
On Error Resume Next
Set oWord = GetObject(,"Word.Application")
If oWord Is Nothing Then
'Word isn't running so document can't be open
Else
If oWord.Documents.Count = 0 Then
'No documents open so this one can't be
Else
For Each oDoc in oWord.Documents
If oDoc.Name = strDocName Then
blDocIsOpen = True
Exit For
End If
Next
End If
Set oWord = Nothing
End If
On Error Goto 0

If you want to be 100% sure you need to allow for the possibility of
more than one instance of Word being present. IIRC that involves using
Windows API calls to iterate through all the windows.
 
Thanks, John.

Just what I was looking for.

John Nurick said:
Hi Dale,

For a simple but not entirely reliable approach you could try something
like this air code:

Dim oWord As Object
Dim oDoc As Object
Dim blDocIsOpen As Boolean
Dim strDocName As String

blDocIsOpen = False
strDocName = "C:\folder\file.doc"
On Error Resume Next
Set oWord = GetObject(,"Word.Application")
If oWord Is Nothing Then
'Word isn't running so document can't be open
Else
If oWord.Documents.Count = 0 Then
'No documents open so this one can't be
Else
For Each oDoc in oWord.Documents
If oDoc.Name = strDocName Then
blDocIsOpen = True
Exit For
End If
Next
End If
Set oWord = Nothing
End If
On Error Goto 0

If you want to be 100% sure you need to allow for the possibility of
more than one instance of Word being present. IIRC that involves using
Windows API calls to iterate through all the windows.
 
Back
Top