|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
* 楼主,请做好原文档备份后试用下面的宏:(注意:不要先打开文档,要先运行宏。请核对是否正确。)
- Sub DIR_循环遍历文件夹_删除关键词()
- On Error Resume Next
- Dim arr, a As Paragraph, j&, r As Range
- arr = Array("校长", "班主任", "老师")
- Dim objShell As Object, objFolder As Object, SearchPath$, DicList As Object, FileList As Object, Key, NowDic$, NowFile$, i&, FileName, FilePath, doc As Document, x&
- Set objShell = CreateObject("Shell.Application")
- Set objFolder = objShell.BrowseForFolder(0, "请选择文件夹!", 0, 0)
- SearchPath = objFolder.self.Path & ""
- Set objShell = Nothing
- Set objFolder = Nothing
- If MsgBox("请确认!是否处理文件夹 " & SearchPath & " ?", 4 + 16) = vbNo Then Exit Sub
- Set DicList = CreateObject("Scripting.Dictionary")
- Set FileList = CreateObject("Scripting.Dictionary")
- DicList.Add SearchPath, ""
- i = 0
- Do While i < DicList.Count
- Key = DicList.keys
- NowDic = Dir(Key(i), vbDirectory)
- Do While NowDic <> ""
- If (NowDic <> ".") And (NowDic <> "..") Then
- If (GetAttr(Key(i) & NowDic) And vbDirectory) = vbDirectory Then DicList.Add Key(i) & NowDic & "", ""
- End If
- NowDic = Dir()
- Loop
- i = i + 1
- Loop
- For Each Key In DicList.keys
- NowFile = Dir(Key)
- Do While NowFile <> ""
- FileList.Add NowFile, Key
- NowFile = Dir()
- Loop
- Next
- i = 0
- FileName = FileList.keys
- FilePath = FileList.Items
- Do While i < FileList.Count
- If FilePath(i) & FileName(i) Like "*.doc*" Then
- Set doc = Documents.Open(FileName:=FilePath(i) & FileName(i), Visible:=True)
- '
- With doc
- If .Paragraphs.Count >= 5 Then
- Set r = .Range(Start:=.Paragraphs(2).Range.Start, End:=.Paragraphs(5).Range.End)
- For j = 0 To UBound(arr)
- For Each a In r.Paragraphs
- If Len(r) = 0 Then Exit For
- With a.Range
- If Not .Information(12) Then
- If .Text Like "*" & arr(j) & "*" Then .Delete
- End If
- End With
- Next
- Next j
- End If
- End With
- '
- doc.Close SaveChanges:=wdSaveChanges
- x = x + 1
- End If
- i = i + 1
- Loop
- Set DicList = Nothing
- Set FileList = Nothing
- MsgBox "文件夹包含 " & i & " 个文件!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
- End Sub
复制代码 |
|