【问题标题】:How to check if a Word file is open from Excel VBA?如何检查 Word 文件是否从 Excel VBA 打开?
【发布时间】:2022-11-22 02:58:49
【问题描述】:

我在谷歌中搜索了确切的术语,它吐出多个结果。 我尝试了至少 4-5 个。 没有工作。根据函数的不同,它要么全为 TRUE,要么全为 FALSE,但永远不会正确。

除了不了解这些功能应该如何工作(这将是次要端点)之外,如果有人可以引导我到主要端点(检查是否从 Excel VBA 打开 Word 文档),我将非常感激?

谢谢

【问题讨论】:

    标签: excel excel-formula


    【解决方案1】:

    Word文件是否打开?

    • 如果 Word 应用程序未打开,则无法打开 Word 文件(在 Word 中)。
    • 所以首先检查Word是否打开,然后检查文件是否打开。

    Word 打开了吗?

    Function IsWordOpen() As Boolean
        Dim wdApp As Object
        ' Attempt to reference the word application.
        On Error Resume Next
            Set wdApp = GetObject(, "Word.Application")
        On Error GoTo 0
        IsWordOpen = Not wdApp Is Nothing
    End Function
    
    Sub IsWordOpenTEST()
        Debug.Print IsWordOpen
    End Sub
    

    参考字

    Function RefWord() As Object
        Dim wdApp As Object
        ' Attempt to reference the word application.
        On Error Resume Next
            Set wdApp = GetObject(, "Word.Application")
        On Error GoTo 0
        If Not wdApp Is Nothing Then Set RefWord = wdApp
    End Function
    
    Sub RefWordTEST()
        ' Reference the word application.
        Dim wdApp As Object: Set wdApp = RefWord
        If wdApp Is Nothing Then
            MsgBox "Word is not open", vbExclamation
            Exit Sub
        End If
        ' Print the names of open files.
        Dim wdDoc As Object
        For Each wdDoc In wdApp.Documents
            Debug.Print wdDoc.Name
        Next wdDoc
    End Sub
    

    Word文件是否打开?

    ' Uses the RefWord function.
    Function IsWordFileOpen( _
        ByVal WordFileName As String) _
    As Boolean
        Dim wdApp As Object: Set wdApp = RefWord
        If wdApp Is Nothing Then Exit Function
        
        Dim wdDoc As Object
        On Error Resume Next
            ' Attempt to reference the word document.
            Set wdDoc = wdApp.Documents(WordFileName)
        On Error GoTo 0
        IsWordFileOpen = Not wdDoc Is Nothing
    End Function
    
    Sub IsWordFileOpenTEST()
        Debug.Print IsWordFileOpen("Test.docx")
    End Sub
    

    Word 文件是否打开(独立)?

    ' Stand-Alone
    Function IsWordFileOpenCompact( _
        ByVal WordFileName As String) _
    As Boolean
        Dim wdApp As Object
        ' Attempt to reference the word application.
        On Error Resume Next
            Set wdApp = GetObject(, "Word.Application")
        On Error GoTo 0
        If wdApp Is Nothing Then Exit Function
        
        Dim wdDoc As Object
        On Error Resume Next
            ' Attempt to reference the word document.
            Set wdDoc = wdApp.Documents(WordFileName)
        On Error GoTo 0
        IsWordFileOpen = Not wdDoc Is Nothing
    End Function
    
    Sub IsWordFileOpenCompactTEST()
        Debug.Print IsWordFileOpenCompact("Test.docx")
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2019-04-29
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      相关资源
      最近更新 更多