|
* 请注意:如果文档要求按顺序打印,少量的话可以手动在文件名前加 1.2.3 等这样的序号,然后再合并。
- Sub 批量合并()
- '更新/2018-12-4/TEST-OK/彻底定稿!
- On Error Resume Next
- Dim fd As FileDialog, i&, doc As Document, p$, t&, j&, s As Section, k&, n&, m&, c&
- Set fd = Application.FileDialog(msoFileDialogFolderPicker)
- If fd.Show = -1 Then p = fd.SelectedItems(1) Else Exit Sub
- Set fd = Nothing
- If MsgBox("是否合并文件夹 " & p & " ?", 4 + 48) = vbNo Then End
- If MsgBox("<是>:Word 文档(*.doc) <否>:文本文档(*.txt)", 4 + 48) = vbYes Then t = 0 Else t = 1
- If MsgBox("请选择分隔符!——<是>:分节符 <否>:分页符", 4 + 48) = vbYes Then j = 1 Else j = 0
- If j = 1 Then
- If MsgBox("每节页码!——<是>:重排 <否>:顺延", 4 + 48) = vbYes Then k = 1 Else k = 2
- Else
- k = 2
- End If
- 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), Visible:=False)
- Else
- Set doc = Documents.Open(FileName:=.FoundFiles(i), Encoding:=936, Visible:=False)
- End If
- doc.Content.Copy
- doc.Close
- Selection.EndKey 6
- Selection.Paste
- ActiveDocument.Characters(1).Copy
- If j = 1 Then
- Selection.InsertBreak Type:=wdSectionBreakNextPage
- Else
- Selection.InsertBreak Type:=wdPageBreak
- End If
- Next i
- MsgBox "合并完毕!共合并 " & .FoundFiles.Count & " 个文件!", 0 + 64
- Else
- MsgBox "未发现文件!", 0 + 16
- End If
- End With
- With ActiveDocument
- .Characters.Last.Previous.Delete
- .Characters.Last.Previous.Delete
- '重排页码
- For Each s In .Sections
- s.Range.Select
- With Selection.Sections(1).Headers(1).PageNumbers
- .NumberStyle = wdPageNumberStyleNumberInDash
- If k = 1 Then .RestartNumberingAtSection = True Else .RestartNumberingAtSection = False
- .StartingNumber = 1
- End With
- Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True
- ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
- Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter.LinkToPrevious
- ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
- Next
- Selection.HomeKey 6
- '奇数加页
- Do
- For Each s In .Sections
- n = s.Range.Information(3)
- n = n - m
- m = m + n
- If n Mod 2 = 1 Then
- s.Range.Characters.Last.InsertBreak Type:=wdPageBreak
- n = 0
- m = 0
- c = 1
- Exit For
- Else
- c = 0
- End If
- Next
- Loop Until c = 0
- End With
- End Sub
复制代码 |
|