VBA 判断Word进程是否存在和当前word文档是否打开

    最近写了点关于word的程序,要判断word程序是否存在。现提供给大家分享(vb6):

    首先要引用microsoft word 11.0 object library(即系统安装了office 2003),具体代码如下

 

Private Function WordisExist(ByVal FilePath As String, ByVal DocName As String) As Integer
  If Dir(FilePath) = "" Then
  WordisExist = 3
' Debug.Print "Word文件不存在"
  Exit Function
  End If
  ' 搜索word文档是否已经打开
  On Error GoTo ERROR0
  For Each Doc In Word.Application.Documents
  If Doc.Name = DocName Then
  Found = True
  Exit For
  End If
  Next
  If Found Then
' MsgBox ("要打开的word文档已经打开")
  WordisExist = 1
  Exit Function
  End If
ERROR0:
  If Err = 0 Then
' MsgBox "将所用的word文档和进程关闭"
  WordisExist = 2
  Exit Function
  Else
  Err.Clear
  End If
  WordisExist = 0
End Function

你可能感兴趣的:(VBA 判断Word进程是否存在和当前word文档是否打开)