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.