|
楼主标题挺吓人,其实就是批量合并。——楼上代码很不错,就是变量未定义。
我给楼主提供一个《批量合并》宏:
- Sub 循环遍历文件夹_批量合并()
- 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
- If MsgBox("是否合并 Word 文档(*.doc)?(否则合并文本文档(*.txt))", vbYesNo + vbExclamation, "循环遍历文件夹_批量合并") = vbYes Then t = 0 Else t = 1
- If MsgBox("合并文档之间是否插入分页符?", vbYesNo + vbExclamation, "循环遍历文件夹_批量合并") = vbYes Then s = 1 Else s = 0
- 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 "合并完毕!共合并 " & .FoundFiles.Count & " 个文件!文档尚未保存,请自行保存!", vbOKOnly + vbExclamation, "循环遍历文件夹_批量合并"
- Else
- MsgBox "未发现文件!", vbOKOnly + vbCritical, "循环遍历文件夹_批量合并"
- End If
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|