请认真阅读代码注释内容。 '* +++++++++++++++++++++++++++++ '* Created By SHOUROU@ExcelHome 2008-4-9 19:12:21 '仅测试于System: Windows NT Word: 11.0 Language: 2052 '№ 0390^The Code CopyIn [ThisDocument-ThisDocument]^' '* ----------------------------- Sub mySeach() '搜索指定文档正文内容或者文件属性中包含关键字的Word文档 '在默认情况下,程序将一一打开这些文件,除非用户在打开对话框中选择的打开 '时,程序将退出运行!!请注意是按打开时退出,而非取消时退出 '如果用户没有响应打开对话框(默认是3秒)后程序继续执行 Dim FS As FileSearch, myFindText As String Dim myFolder As String, myDialog As FileDialog Dim i As Long, N As Long, myFileName As String Dim myDoc As Document, DigOpen As Dialog On Error Resume Next myFindText = VBA.InputBox("请输入需要查找的关键字", , "用人工方法合成同位素") If myFindText = "" Then Exit Sub Set myDialog = Application.FileDialog(msoFileDialogFolderPicker) With myDialog .Title = "请选择一个您需要搜索的文件夹" If .Show <> -1 Then Exit Sub myFolder = .InitialFileName End With Set myDialog = Nothing Set FS = Application.FileSearch With FS .NewSearch .LookIn = myFolder .SearchSubFolders = True .FileName = "*.doc" .TextOrProperty = myFindText If .Execute() > 0 Then N = .FoundFiles.Count MsgBox "在接下来出来的打开对话框中,如果您按下打开键,程序将退出运行,反之(默认或者无操作)则继续!", vbExclamation, "ExcelHome" For i = 1 To N myFileName = CStr(.FoundFiles(i)) Application.StatusBar = i & "/" & N Set DigOpen = Word.Dialogs(wdDialogFileOpen) With DigOpen .Name = myFileName If .Show(TimeOut:=3000) <> 0 Then ActiveDocument.Close: Exit Sub End With Set myDoc = Word.Documents.Open(FileName:=myFileName, Visible:=True) With myDoc.ActiveWindow.Selection.Find .Text = myFindText .MatchWildcards = False If .Execute = False Then myDoc.Close End With Next i MsgBox "程序已经运行结束!", vbInformation, "ExcelHome" Else MsgBox "Microsoft Word在" & myFolder & "文件夹中没有找到包含" & myFindText & "关键字的Word文档!", vbInformation, "ExcelHome" End If End With End Sub '---------------------- 请测试!
[此贴子已经被作者于2008-4-9 19:12:47编辑过] |