【发布时间】:2022-10-24 14:43:11
【问题描述】:
比如有3个word文件(名称:word_1、word_2、word_3),我想把所有内容合并到word_1中。
合并前:
Content in word_1:
1.1.1 Hello World
testing text 111
1.1.2 Hello America
testing text 222
1.1.3 Hello USA
1.1.4 Hello Korea
1.1.5 Hello Japan
Content in word_2:
1.1.3 Hello USA
testing text 333
1.1.4 Hello Korea
testing text 444
Content in word_3:
1.1.5 Hello Japan
testing text 888
1.1.6 Hello Germany
testing text 999
合并后:
1.1.1 Hello World
testing text 111
1.1.2 Hello America
testing text 222
1.1.3 Hello USA
testing text 333
1.1.4 Hello Korea
testing text 444
1.1.5 Hello Japan
testing text 888
基本上它是由主题旁边的 ID 合并(即 1.X.X,X.X.X),我假设没有重复的 ID,每个 ID 都是唯一的。目前我找到了一些资源作为下面的代码,我只能将word文件合并到另一个word文件。非常感谢你的帮助。如果能用python实现也可以。
Option Explicit
Sub MergeFilesInAFolderlntoOneDoc()
Dim dlgFile As FileDialog
Dim wdApp As Object, oRng As Object
Dim objDoc As Object, objNewDoc As Object
Dim StrFolder As String, strFile As String
Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker)
With dlgFile
If .Show = -1 Then
StrFolder = dlgFile.SelectedItems.Item(1) & Chr(92)
Else
MsgBox ("No folder is selected!")
Exit Sub
End If
End With
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
strFile = Dir(StrFolder & "*.docx", vbNormal)
With wdApp
.Visible = True
Set objNewDoc = .Documents.Add
While strFile <> ""
Set objDoc = .Documents.Open(FileName:=StrFolder & strFile)
With objNewDoc
Set oRng = .Range
If Len(oRng) > 1 Then
oRng.Collapse 0
oRng.InsertBreak 7
End If
oRng.Collapse 0
oRng.FormattedText = objDoc.Range.FormattedText
objDoc.Close 0
End With
DoEvents
strFile = Dir()
Wend
objNewDoc.Activate
End With
End Sub
【问题讨论】: