Article ID: 182790
Article Last Modified on 8/16/2006
Sub IsDocAlreadyOpen()
Dim sDocToOpen As String
Dim dDoc As Document
' Replace the following example document path and file
' with the path and file you want to open.
sDocToOpen = "Mac HD:My Documents:MyDoc.Doc"
' Loop through all open documents.
For Each dDoc In Documents
'Test for match.
If UCase(sDocToOpen) = UCase(dDoc.Path & ":" & dDoc.Name) Then
' If open, display a message.
MsgBox sDocToOpen & " is already open."
Exit Sub
Else
On Error Resume Next
' Open the document.
Documents.Open (sDocToOpen)
' If the document does not exist or the path/name is
' incorrect an error will occur. This error trap
' displays the error information.
If Err <> 0 Then
MsgBox Err.Description
End If
Exit Sub
End If
' Loop to next document.
Next dDoc
End Sub
For additional information, please see the following article in the
Microsoft Knowledge Base:
181058 OFF98: How to Run Sample Code from Knowledge Base Articles
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon vb vba vbe
Keywords: kbcode kbdtacode kbhowto kbmacroexample kbprogramming KB182790