如何判断 word文件已经被打开了?---顶者有分!
如何判断 word文件已经被打开了?
我是这样写的。
Public Function WordFileOpened(strFileName As String) As Boolean
WordFileOpened = False
On Error Resume Next
Dim docAPP As Object
Dim doc As Object
Set docAPP = GetObject(, "Word.Application ")
If Err.Number <> 0 Then Exit Function
For Each doc In docAPP.Documents
Debug.Print doc.FullName
If doc.FullName = strFileName Then
WordFileOpened = True
Exit For
End If
Next doc
Set doc = Nothing
Set docAPP = Nothing
End Function
问题是:当只有一个word文件被打开时,函数正确。
当有2个或多个word文件文件被打开时,判断某个文件时,就不能正确了。
好像 docAPP.Documents 总是只有一个,为什么??
[解决办法]
Private Sub Form_Load()
Debug.Print isOpenDOC( "d:\1.doc ")
End Sub
Public Function isOpenDOC(strpath As String) As Boolean
isOpenDOC = False
On Error GoTo err1
Open strpath For Input Lock Read As #1
Close #1
Exit Function
err1:
Close #1
isOpenDOC = True
End Function
[解决办法]
GetObject( "word.application ")
如果出错,表示,现在没有开启的Word程序.
这个要用到On Error 去捕捉.
如果开多个文件档的话,Documents.Count应该可以读到有多少个.
但有一个情况就是多个Appliction实例时(多个Word进程),就要累加App对于Documents了.