|
楼主,请备份原文件后,试用下面的宏(合并后第1页的“分页符”请自行删除;合并后文档并未存盘,请自行保存/存盘;合并后请检查是否正确,不正确请等待各位高人相助。我不敢保证是否正确。双击文件夹直到进入最终所需要打开的文件夹后按确定):
- Sub test批量合并()
- 'code by 捷克人
- On Error Resume Next
- Dim fd As FileDialog, p$, doc As Document, n&
- Set fd = Application.FileDialog(msoFileDialogFolderPicker)
- If fd.Show = -1 Then p = fd.SelectedItems(1) Else End
- Set fd = Nothing
- If MsgBox("是否处理文件夹 " & p & " ?", 4 + 48) = vbNo Then End
- Documents.Add
- Dim FileNameWithPath As Variant, ListOfFilenamesWithParh As New Collection
- Call FileSearchByHavrda(ListOfFilenamesWithParh, p, "*.docx", True)
- For Each FileNameWithPath In ListOfFilenamesWithParh
- Set doc = Documents.Open(FileName:=FileNameWithPath)
- With doc
- .Content.Copy
- .Close
- With Selection
- .EndKey 6
- .InsertBreak Type:=wdPageBreak
- .Paste
- End With
- ActiveDocument.Characters(1).Copy
- End With
- n = n + 1
- Next FileNameWithPath
- If ListOfFilenamesWithParh.Count = 0 Then MsgBox "File not found!"
- MsgBox "处理完毕!共处理 " & n & " 个文档!", 0 + 48
- End Sub
- Sub FileSearchByHavrda(pFoundFiles As Collection, pPath As String, pMask As String, pIncludeSubdirectories As Boolean)
- Dim DirFile As String, CollectionItem As Variant, SubDirCollection As New Collection
- pPath = Trim(pPath)
- If Right(pPath, 1) <> "" Then pPath = pPath & ""
- DirFile = Dir(pPath & pMask)
- Do While DirFile <> ""
- pFoundFiles.Add pPath & DirFile
- DirFile = Dir
- Loop
- If Not pIncludeSubdirectories Then Exit Sub
- DirFile = Dir(pPath & "*", vbDirectory)
- Do While DirFile <> ""
- If DirFile <> "." And DirFile <> ".." Then If ((GetAttr(pPath & DirFile) And vbDirectory) = 16) Then SubDirCollection.Add pPath & DirFile
- DirFile = Dir
- Loop
- For Each CollectionItem In SubDirCollection
- Call FileSearchByHavrda(pFoundFiles, CStr(CollectionItem), pMask, pIncludeSubdirectories)
- Next
- End Sub
复制代码 |
|