Article ID: 159345
Article Last Modified on 1/19/2007
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 = "C:\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:
173707 OFF97: 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: kbdtacode kbhowto kbprogramming KB159345