[英]How to check if a Word file is open from Excel VBA?
I searched for the exact term in Google, it spits out multiple results.我在谷歌中搜索了确切的术语,它吐出多个结果。 I tried al least 4-5 of them.
我尝试了至少 4-5 个。 None works.
没有工作。 It is either all TRUE or all FALSE depending on the function, but it is never correct.
根据 function,它要么全部为 TRUE,要么全部为 FALSE,但它永远不会正确。
In addition to not understanding how those functions are supposed to work (which would be a secondary endpoint) I would be really grateful if someone could lead me to primary endpoint (which is checking if a Word document is open from Excel VBA)?除了不了解这些功能应该如何工作(这将是次要端点)之外,如果有人可以引导我到主要端点(检查 Word 文档是否从 Excel VBA 打开),我将非常感激?
Thanks谢谢
Is Word open? 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
Reference Word参考字
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
Is Word file open? 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
Is Word file open (stand-alone)? Word 文件是否打开(独立)?
True
then you know the following:True
,那么您知道以下内容:
Test.docx
is open.Test.docx
的文件已打开。False
, it gets even worse ie then you don't know the following:False
,情况会变得更糟,即您不知道以下内容:
' 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
IsWordFileOpenCompact = Not wdDoc Is Nothing
End Function
Sub IsWordFileOpenCompactTEST()
Debug.Print IsWordFileOpenCompact("Test.docx")
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.