How to check whether document is open?

A

avkokin

Hello.
I need to check whether document is open. For example, I open Word but
not create blank document. Next I launch some macro which should work
with document. I get the error 4248. How to organize such check-up
into start macro?
 
G

George Lee

One common way to check if a file is available is to actually perform some
operation on the file but trap the error code (see below). You can do this
early on to make sure the file is opened later in the routine.

Public Sub CheckForOpenDocument()
On Error GoTo MyErrorHandler

Dim gWordApp As Word.Application
Set gWordApp = Application

Dim fileName As String
fileName = "c:\test.doc" 'Or however you get the file name

On Error Resume Next
Dim isDocOpened As Document
Set isDocOpened = gWordApp.Documents(fileName)
If Err.Number <> 0 Then
MsgBox "File could not be opened."
'Other processing here, such as
'Set isDocOpened = gWordApp.Documents.Open(fileName)
'More error checking here
End If
On Error GoTo MyErrorHandler

Exit Sub

MyErrorHandler:
MsgBox "CheckForOpenDocument" & vbCrLf & vbCrLf & "Err = " & Err.Number
& vbCrLf & "Description: " & Err.Description
End Sub

There are some warnings, though. First, in some cases being opened isn't
enough, it has to the visible as well. Second, and this big, is if the file
is opened in another instance of Word. The current instance of Word is
independent of other instances. This case is harder to fix. This case is
usally seen with being told the file is already opened by another user,
namely yourself.
 
S

Steve Yandl

Check out the 'Tasks' property of the Word application. It will return a
collection of the tasks running. For each task, the name property is the
friendly name (the name that appears in the title bar of the Window) rather
than the often less informative process name. Using the InStr function on
the name property for each task in the collection, you should be able to
determine if a given document is open. You can also check the 'visible'
property to determine if the window is visible or hidden. If you're so
inclined, there is also a close method that would allow you to shut down a
given task.


Steve
 
A

avkokin

Hello George.
Thank you, but when I launch Word I not have not one documents is open
- only application. And I should check it.

Hello Steve. Can you whether show one example? Sorry, I learned VBA
only..
Thank you very much.
 
A

avkokin

Hello Steve. I can use next code:
If ActiveDocument Is Nothing Then
MsgBox "Not open document"
Else

I will work if only active line "On Error Resume Next".
But it is uncorrect. If this line is comment then I get error. What I
should do more? How to use InStr for Application?
Thank you very much.
 
D

Doug Robbins - Word MVP

Dim docname As String
Dim flag As Boolean
flag = False
docname = "Drive:\Path\Filename.doc"
With Application
For i = 1 To .Tasks.Count
If InStr(.Tasks(i).Name, docname) > 0 Then
flag = True
Exit Sub
End If
Next i
If flag = True Then
MsgBox docname & " is open."
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

To determine if any documents are open, use

Application.Documents.Count

It will return 0 if there are no documents.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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