|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
楼主,公文要求不必有 01这样的发文字号,只来1号即可,下面是〈循环遍历文件夹--报告编号〉宏,请备份原文件夹后应用(把所有要编号的文件放到一个文件夹中,打包备份后应用此宏):
- Sub 报告编号()
- On Error Resume Next
- Dim fd As FileDialog, i As Long, doc As Document, p As String, t As Long, s As Long, j As Paragraph
- 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
- With Application.FileSearch
- .NewSearch
- .LookIn = p
- .SearchSubFolders = True
- .FileName = "*.doc"
- If .Execute > 0 Then
- For i = 1 To .FoundFiles.Count
- Set doc = Documents.Open(FileName:=.FoundFiles(i))
- '---------------------------------------------------------
- If ActiveDocument.Paragraphs(2).Range.Characters.Last.Previous.Text = "号" Then
- ActiveDocument.Paragraphs(2).Range.Characters.Last.Previous.Select
- Selection.Font.Color = wdColorRed '红色
- Do
- Selection.MoveStart unit:=wdCharacter, Count:=-1
- Loop Until Selection.Characters.First Like "[! ]"
- Selection = Replace(Selection, " ", "")
- Selection = Replace(Selection, " ", "")
- Selection.Characters.Last.InsertBefore Text:=i
- End If
- '---------------------------------------------------------
- doc.Close savechanges:=wdSaveChanges
- Next i
- MsgBox "处理完毕!共处理 " & .FoundFiles.Count & " 个文件!", vbOKOnly + vbExclamation, "循环遍历文件夹_报告编号"
- Else
- MsgBox "未发现文件!", vbOKOnly + vbCritical, "循环遍历文件夹_报告编号"
- End If
- End With
- End Sub
复制代码 |
|