简体   繁体   English

用VBA以单词形式打开doc或docx

[英]Opening doc or docx in word with vba

I want to open a word document for exclusive use, however my code fails sometimes. 我想打开一个Word文档以专用,但是我的代码有时会失败。 We have a few cases: 我们有几种情况:

  • The document is called .doc 该文档称为.doc
  • The document is called .docx 该文档称为.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. 后两种情况在我的函数中应返回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

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() . 以下是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. 也许有一种解决方法:找出IsFileLocked()什么条件下报告错误结果。 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. 也许您可以通过要求所有同事将文档自动保存间隔设置为1分钟来找到解决方法,从而减少这种情况的发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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