以下是引用vba_lover在2006-11-3 14:48:06的发言:我想表达是: 1)在Word中,我想对另一个Word文档的内容进行一下修改,所以我想先确认一下在系统中那个我要修改的Word文档是否已经打开了 2)在Excel中或其他Office中,我想知道当前系统中有没有打开了的Word文档 请帮忙给个提示!非常感谢! 问题1,请参考: '* +++++++++++++++++++++++++++++ '* Created By SHOUROU@ExcelHome 2006-11-4 7:48:32 '仅测试于System: Windows NT Word: 11.0 Language: 2052 '№ 0094^The Code CopyIn [ThisDocument-ThisDocument]^' '* ----------------------------- Option Explicit
Sub FindmyDoc() Dim oDoc As Document, myDocName As String, blnFound As Boolean myDocName = "C:\Documents and Settings\shourou\My Documents\Temp\my.doc" For Each oDoc In Documents If VBA.UCase(oDoc.FullName) = VBA.UCase(myDocName) Then blnFound = True: Exit For Next If blnFound = True Then MsgBox "目标文档已经打开!", vbInformation, "Microsoft Word" Else MsgBox "目标文档没有打开!", vbInformation, "Microsoft Word" End If End Sub '---------------------- Sub FindmyDoc2() Dim myDocName As String On Error Resume Next myDocName = "C:\Documents and Settings\shourou\My Documents\Temp\my.doc" Documents(myDocName).Activate If Err.Number = 4160 Then Err.Clear MsgBox "目标文档没有打开!", vbInformation, "Microsoft Word" Else MsgBox "目标文档已经打开!", vbInformation, "Microsoft Word" End If End Sub '----------------------
问题2,请参考(EXCEL中,其它OFFICE程序类同) Option Explicit Sub Example() '在Excel中,使用后期绑定方式创建一个Word对象 Dim myWd As Object, oDoc As Object On Error Resume Next Set myWd = GetObject(, "Word.Application") If Err.Number <> 0 Then Err.Clear Set myWd = CreateObject("Word.Application") End If For Each oDoc In myWd.Documents MsgBox oDoc.Name Next End Sub
|