判断指定的WORD文档是否被打开的函数代码如下:
Function WordDocIsOpen(ByVal strDocName As String) As Boolean Dim objWordApp As Object Dim objWordDoc As Object On Error Resume Next '此句不能少 strDocName = UCase(strDocName) Set objWordApp = GetObject(,"Word.Application") For Each objWordDoc In objWordApp.Documents If UCase(objWordDoc.FullName) = strDocName Then WordDocIsOpen = True Exit For End If Next Set objWordDoc = Nothing Set objWordApp = Nothing End Function
调用代码:
Private Sub main() If WordDocIsOpen("e:/2.doc") Then MsgBox "该文档已被打开" Else MsgBox "该文档未被打开" End If End Sub
读者只要稍加留意,可能早已注意到,其实只要把以前代码中的Word.Application改为Excel.Application,就可以判断指定的Excel文档是否被打开了。