Re: Checking if a Word file is open

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: GoGoGadgetScott (gogogadgetscott_at_hotmail.com)
Date: 04/26/04


Date: Mon, 26 Apr 2004 00:00:54 -0400

Function DocOpen(sFile)
        'Get an existing instance of the Word object, if one exists
        On Error Resume Next
        Set oWord = GetObject(, "Word.Application")
        'Word is not open
        If Err.Number = 429 Then Exit Function
        On Error GoTo 0
        'If an instance of word is found we can loop though the collection of
documents
        For Each Document in oWord.Documents
                If Document.FullName = sFile Then
                        'Match is made, documnet is open in Word
                        DocOpen = True
                        'Found what we were looking for, lets get out of here
                        Exit For
                End If
        Next
        'We can now control the open document
        If DocOpen Then
                'This works becouse we used the Exit for
                Document.Close
                'Else would could find our document like this:
                'oWord.Documents(sFile).Close
                'Don't use both, we cannot close a doc twice
        End If
        'Close up word if there are no open docs
        If oWord.Documents.Count = 0 Then oWord.Quit
        'Clean up out mess
        Set oWord = Nothing
End Function

'Full path of file we are checking to determine if open
sFile = "C:\Untitled.doc"
'Output results
If DocOpen(sFile) Then
        MsgBox sFile & " was open, but now closed"
Else
        MsgBox sFile & " is not open"
End If

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/