|
楼主 |
发表于 2016-1-11 16:24
|
显示全部楼层
还是 139 细心啊!现在不判断多少文件了,直接“是、否”选择:
- Sub 循环遍历文件夹_文件搜索_超链接()
- On Error Resume Next
- Dim fd As FileDialog, i As Long, doc As Document, p As String, j As String, k As Long, q As String, r As String, s As String, t As String, a As String
- Set fd = Application.FileDialog(msoFileDialogFolderPicker)
- If fd.Show = -1 Then p = fd.SelectedItems(1) Else Exit Sub
- Set fd = Nothing
- If MsgBox("是否搜索文件夹 " & p & " ?", vbYesNo + vbExclamation, "循环遍历文件夹_文件搜索") = vbNo Then Exit Sub
- j = MsgBox("是:<文件名关键词> 否:<正文关键词> 取消:<文件名关键词+正文关键词>", vbYesNoCancel + vbExclamation, "请选择文件搜索模式!")
- If j = vbYes Then
- k = 1: q = "<文件名关键词>": r = "任职"
- ElseIf j = vbNo Then
- k = 2: q = "<正文关键词>": r = "秦楚楚"
- Else
- k = 3: q = "<文件名关键词+正文关键词>": r = "任职,秦楚楚"
- End If
- reipt:
- a = InputBox("如果同时输入<文件名关键词>+<正文关键词>,必须以中文逗号分隔!如:“任职,秦楚楚”", "请输入" & q & "搜索文件!", r)
- If a = "" Then Exit Sub
- If k = 1 Then
- If a Like "*,*" Then GoTo reipt
- s = a
- ElseIf k = 2 Then
- If a Like "*,*" Then GoTo reipt
- t = a
- ElseIf k = 3 Then
- If Not (a Like "*,*") Then GoTo reipt
- s = Left(a, InStr(a, ",") - 1): t = Mid(a, InStr(a, ",") + 1)
- End If
- Set doc = Documents.Add
- With Application.FileSearch
- .NewSearch
- .LookIn = p
- .SearchSubFolders = True
- If k = 1 Then
- .FileName = "*" & s & "*.doc"
- ElseIf k = 2 Then
- .FileName = "*.doc"
- .TextOrProperty = t
- ElseIf k = 3 Then
- .FileName = "*" & s & "*.doc"
- .TextOrProperty = t
- End If
- If .Execute > 0 Then
- For i = 1 To .FoundFiles.Count
- doc.Content.InsertAfter Text:=.FoundFiles(i) & vbCr
- Next i
- If MsgBox("搜索完毕!共发现 " & .FoundFiles.Count & " 个文件!——是否打开搜索的文件?", vbYesNo + vbExclamation, "循环遍历文件夹_文件搜索") = vbYes Then
- For i = 1 To .FoundFiles.Count
- Documents.Open FileName:=.FoundFiles(i)
- Next i
- Else
- Options.CtrlClickHyperlinkToOpen = False '单击超链接打开文件
- Dim v As Paragraph
- For Each v In ActiveDocument.Paragraphs
- v.Range.Select
- Selection.MoveEnd unit:=wdCharacter, Count:=-1
- v.Range.Hyperlinks.Add Anchor:=Selection.Range, Address:=Selection.Text
- Next
- End If
- ' MsgBox "搜索完毕!共发现 " & .FoundFiles.Count & " 个文件!", vbOKOnly + vbExclamation, "循环遍历文件夹_文件搜索"
- Else
- MsgBox "未发现文件!", vbOKOnly + vbCritical, "循环遍历文件夹_文件搜索"
- End If
- End With
- End Sub
复制代码 |
|