|
* 楼主,请备份文件后试用下面的宏:(注意:请手动复制 Excel 文件中的数据到一个新建 Word 文件中,然后执行宏,其它 Word 文件放到一个文件夹中)
- Sub DIR_循环遍历文件夹_查找数字_设置格式()
- Dim arr, s$, t$, h&, j&
- h = ActiveDocument.Tables(1).Rows.Count
- For j = 0 To h - 1
- s = ActiveDocument.Tables(1).Range.Cells(j + 1).Range.Text
- s = Left(s, Len(s) - 2)
- t = t & "," & s
- Next j
- t = Right(t, Len(t) - 1)
- arr = Split(t, ",")
- ActiveDocument.Close savechanges:=wdDoNotSaveChanges
- '''
- On Error Resume Next
- 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))
- For j = 0 To h - 1
- With ActiveDocument.Content.Find
- .ClearFormatting
- .Text = arr(j)
- .Forward = True
- .MatchWildcards = True
- Do While .Execute
- With .Parent
- With .Font
- .NameAscii = "Arial"
- .Size = 12
- .Bold = True
- .Color = wdColorRed
- End With
- .Start = .End
- End With
- Loop
- End With
- Next j
- 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
复制代码 |
|