简体   繁体   中英

Opening doc or docx in word with vba

I want to open a word document for exclusive use, however my code fails sometimes. We have a few cases:

  • The document is called .doc
  • The document is called .docx
  • The document is already open
  • The document is open by another user
  • The document does not exist

The last two cases should return false in my function.

Function OpenDocument(filename As String) As Boolean
    Dim found As Boolean
    Dim doc As Object
    Dim fileNameCorrected  As String

    found = False
    OpenDocument = True
    'need to figure out if its docx or doc or
    fileNameCorrected = Dir(GetFileNameWithOutExtension(filename) & ".do*")
    filename = FileHandling.GetFolder(filename) & fileNameCorrected

    If fileNameCorrected <> "" Then
        For Each doc In Documents
            If doc.name = fileNameCorrected Then
                found = True
                Exit For
            End If
        Next doc
        If found <> True Then
             ' \\ Only open File if the file is not opened yet
            If Not IsFileLocked(filename) Then
                Documents.Open filename:=filename
            Else
                OpenDocument = False
                Exit Function
            End If
        End If

        Documents(fileNameCorrected).Activate
    Else
        'MsgBox "File doesn't exist. Sorry check ur settings mate"
        OpenDocument = False
    End If
End Function

However it fails here sometimes

            If Not IsFileLocked(filename) Then
                Documents.Open filename:=filename
            Else

the strange thing is it jumps in there when the document already is open – that does not work as desired.

Below is the code for IsFileLocked() .

Function IsFileLocked(sFile As String) As Boolean
    On Error Resume Next

     ' \\ Open the file
    Open sFile For Binary Access Read Write Lock Read Write As #1
     ' \\ Close the file
    Close #1

     ' \\ If error occurs the document if open!
    If Err.Number <> 0 Then
         ' \\ msgbox for demonstration purposes
        'MsgBox Err.description, vbExclamation, "Warning File is opened"

         '\\ Return true and clear error
        IsFileLocked = True
        Err.Clear
    End If
End Function

Any ideas where the problem is or is there a better way to open a document?

From what you describe, under some conditions (not always) the operating system still allows to open file successfully although the file is already open in computer of someone else.

There is not much you can do about that. And logic in your code seems to be correct.

Maybe there is a workaround: find out under what conditions IsFileLocked() reports wrong result. Is it only before the file is first fime saved or auto-saved ? Maybe you can find a workaround then by asking all colleagues to set document auto-saving interval to 1 minute and thus reduce the chance of happening this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM