在打开 Word 文件之前,我想检查该文件是否已经打开。(同时打开多个word文件)主子调用这个函数告诉我它是否打开。
Function FileInWdOpen(DokName As String) As Boolean
Dim wd As Word.Application
Dim wDoc As Word.Document
On Error Resume Next
Set wd = GetObject(, "Word.Application")
On Error GoTo NO_WORD_FOUND
If wd Is Nothing Then
FileInWdOpen = False
End If
For Each wDoc In wd.Documents 'should check for every open word file but doesn't do that
If wDoc.Name = DokName Then 'checks if this file is named like the one I want to check if its open or not
FileInWdOpen = True
Exit Function
End If
Next
FileInWdOpen = False
Exit Function
NO_WORD_FOUND:
FileInWdOpen = False
End Function
当只打开一个 word 文件时,此代码运行良好。如果打开了两个或更多文件,则脚本不起作用。
问题是 for 循环只检查第一个打开的文件。
我不明白为什么它不检查所有打开的文件。我认为可以通过以下方式访问所有文档:
Dim WordApp As Word.Application 'sets an var for the Word Application
Set WordApp = GetObject(, "Word.Application") 'give the var an obj, in this case the Word Application
Dim WordDoc As Word.Document 'sets an var for the singel Word Documents
For Each WordDoc In WordApp.Documents 'for each Document in Dokuments
'code
Next
那么为什么只有第一个文件得到关注呢?