用vba在word中打开doc或docx

用vba在word中打开doc或docx,vba,ms-word,docx,doc,Vba,Ms Word,Docx,Doc,我想打开一个word文档供独占使用,但是我的代码有时会失败。我们有几个案例: 该文档称为.doc 该文档名为.docx 文档已打开 文档已被其他用户打开 该文档不存在 最后两个案例在我的函数中应该返回false Function OpenDocument(filename As String) As Boolean Dim found As Boolean Dim doc As Object Dim fileNameCorrected As String

我想打开一个word文档供独占使用,但是我的代码有时会失败。我们有几个案例:

  • 该文档称为.doc
  • 该文档名为.docx
  • 文档已打开
  • 文档已被其他用户打开
  • 该文档不存在
最后两个案例在我的函数中应该返回false

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
奇怪的是,当文档已经打开时,它会跳转到那里——这并不能按预期工作

下面是
IsFileLocked()
的代码

函数IsFileLocked(sFile作为字符串)作为布尔值
出错时继续下一步
“\\n打开文件
打开用于二进制访问读写锁定读写为#1的sFile
'\\关闭文件
关闭#1
'\\如果发生错误,打开文档!
如果错误号为0,则
'\\msgbox用于演示
'MsgBox Err.description,vb惊叹号,“警告文件已打开”
“\\Return true和clear error
IsFileLocked=True
呃,明白了
如果结束
端函数

您是否知道问题出在哪里,或者是否有更好的方法打开文档?

根据您的描述,在某些情况下(并非总是),操作系统仍然允许成功打开文件,尽管文件已在其他人的计算机中打开

对此你无能为力。代码中的逻辑似乎是正确的


也许有一个解决方法:找出在什么情况下
IsFileLocked()
报告错误的结果。仅在文件首次保存或自动保存之前?也许你可以通过要求所有同事将文档自动保存间隔设置为1分钟来找到解决方法,从而减少发生这种情况的可能性。

IsFileLocked()
实际上做了什么?你能公布它的来源吗?查看它,您能看到任何可能失败的锁测试条件吗?谢谢,是否可以打开它并执行操作,如果有其他错误代码,则可以计算文件状态?人们通常如何在网络驱动器上打开word文件以获得写入权限?如果其他用户打开了该文档,而您也被允许打开它,那么问题是什么?你们中有人无法保存文档吗?或者您的公司有一个工作流流程,不允许两名员工在一个文档上工作?@miroxlav Word没有excel所具有的共享属性,因此只有一名用户能够保存该文档,这在这种情况下是不合适的。因此,我需要检查word文件是否可写。也许我应该在open file方法中设置revert属性:这样我就可以避免循环查看文档是否已打开或?@skatun–revert:我知道设置revert=true仅对您的计算机有效。若文档已在Word中打开,则其未保存的更改将被丢弃,并再次打开(如使用save=no关闭文档,然后再次打开)。我不确定你是否真的想要这个。
            If Not IsFileLocked(filename) Then
                Documents.Open filename:=filename
            Else
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