|
楼主:
* 请首先备份好要处理的文档/文件夹。
* 再请删除要处理的文件夹中的“排版汇总”文档。
* 试试我的宏吧(也可以不用我的宏,而参考一下),双击选定要处理的文件夹。
- Sub test()
- On Error Resume Next
- Dim fd As FileDialog, i As Long, doc As Document, p As String, t As Long, s As Long
- 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
- Kill p & "排版汇总.doc"
- 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))
- ' doc.Content.Font.Color = wdColorRed '处理单个文档(通用)
- '''
- '删除手动换行符和假段落标记
- doc.Content.Find.Execute findtext:="^l", replacewith:="^p", Replace:=wdReplaceAll
- doc.Content.Find.Execute findtext:="^13", replacewith:="^p", Replace:=wdReplaceAll
- '删除段落首尾空格
- '全选/居中/两端对齐
- '[方法1]
- ' SendKeys "^(aej)", True
- '[方法2]
- ' Selection.WholeStory
- ' Application.Run "CenterPara"
- ' Application.Run "LeftPara"
- '[方法3]
- Selection.WholeStory
- CommandBars.FindControl(ID:=122).Execute
- CommandBars.FindControl(ID:=123).Execute
- '删除空行
- Dim j As Paragraph
- For Each j In ActiveDocument.Paragraphs
- If Len(j.Range) = 1 Then j.Range.Delete
- Next
- '自动编号转文本
- doc.Content.ListFormat.ConvertNumbersToText
- doc.Content.Find.Execute findtext:="^t", replacewith:="", Replace:=wdReplaceAll
- '正文排版
- Selection.WholeStory
- Selection.ClearFormatting
- Selection.ClearFormatting
- With Selection.Font
- .Size = 12
- .Kerning = 0
- .DisableCharacterSpaceGrid = True
- End With
- With Selection.ParagraphFormat
- .LineSpacing = LinesToPoints(1.25)
- .CharacterUnitFirstLineIndent = 2
- .AutoAdjustRightIndent = False
- .DisableLineHeightGrid = True
- End With
-
- doc.Paragraphs(1).Range.Style = wdStyleHeading2
- If doc.Paragraphs(2).Range Like "[!作者:]*" Then doc.Paragraphs(2).Range.InsertBefore Text:="作者:"
- doc.Paragraphs(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
- doc.Paragraphs(2).Range.InsertAfter Text:=vbCr
- '''
- doc.Close savechanges:=wdSaveChanges
- Next i
- ' MsgBox "处理完毕!共处理 " & .FoundFiles.Count & " 个文件!", vbOKOnly + vbExclamation, "循环遍历文件夹_通用"
- Else
- MsgBox "未发现文件!", vbOKOnly + vbCritical, "循环遍历文件夹_通用"
- End If
- End With
- '循环遍历文件夹_批量合并
- t = 0
- s = 1
- Documents.Add
- With Application.FileSearch
- .NewSearch
- .LookIn = p
- .SearchSubFolders = True
- If t = 0 Then .FileName = "*.doc" Else .FileName = "*.txt"
- If .Execute > 0 Then
- For i = 1 To .FoundFiles.Count
- If t = 0 Then Set doc = Documents.Open(FileName:=.FoundFiles(i)) Else Set doc = Documents.Open(FileName:=.FoundFiles(i), Encoding:=936)
- doc.Content.Copy
- doc.Close
- Selection.EndKey Unit:=wdStory
- Selection.Paste
- ActiveDocument.Characters(1).Copy
- If s = 1 Then Selection.InsertBreak Type:=wdPageBreak
- Next i
- If s = 1 Then Selection.TypeBackspace: Selection.TypeBackspace Else Selection.TypeBackspace
- MsgBox "文档已经保存!退出即可!(保存路径:" & p & ")" & "共合并 " & .FoundFiles.Count & " 个文件!", vbOKOnly + vbExclamation, "循环遍历文件夹_批量合并"
- Else
- MsgBox "未发现文件!", vbOKOnly + vbCritical, "循环遍历文件夹_批量合并"
- End If
- End With
- ActiveDocument.SaveAs FileName:=p & "\排版汇总(最新)" & ".doc"
- Selection.HomeKey Unit:=wdStory
-
- End Sub
复制代码 |
|