第二版。加入了合併文檔順序問題的解放方法。
方法1:簡單快捷
把要合併的文件放在同一個文件夾,新建一個word文檔,插入⇒文本⇒對象⇒從文件插入,選擇要插入的文件就可以了。
這種方法存在一個問題:有時候word合併後文件的順序並非你所希望的順序,順序通常莫名其妙,難以預測。所以有了方法2。
方法2:按照文件名的排序合併多個word文檔
(1)打開一個新建的word,按alt+F11進入vba編程環境。
(2)按「插入」-「模塊」。
(3)在新建一個模塊1,把下面一段綠色的文字粘貼上。
Sub CombineDocuments()
On Error Resume Next
'Select Files into Array
Dim dlgOpen As FileDialog
Set dlgOpen =Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
If dlgOpen.SelectedItems.Count = 0 Then
MsgBox "No Files wereselected"
Exit Sub
End If
filetoopenstr = ""
For i = 1 To dlgOpen.SelectedItems.Count
filetoopenstr = filetoopenstr +dlgOpen.SelectedItems(i) + vbTab
Next i
filetoopenstr = Left(filetoopenstr,Len(filetoopenst) - 1)
filestoopen = Split(filetoopenstr,vbTab)
'Sort Array
For i =LBound(filestoopen) To UBound(filestoopen)
For J =LBound(filestoopen) To UBound(filestoopen) - 1
If UCase(filestoopen(J)) > UCase(filestoopen(J + 1)) Then
vehicle = filestoopen(J)
filestoopen(J) = filestoopen(J + 1)
filestoopen(J + 1) = vehicle
End If
Next J
Next i
'Insert Files
For x = LBound(filestoopen)To UBound(filestoopen)
Selection.InsertFilefileName:=filestoopen(x), Range:="", _
ConfirmConversions:=False, Link:=False, Attachment:=False
Next x
End Sub
(4)按F5運行這段話。 word 會提示你選擇要合併的文件,並把所選文件按文件名順序合併。
溫馨提示:如果在一些電腦上,以下兩句是紅的,則把兩段合成一段,去掉underscore"_" Selection.InsertFilefileName:=filestoopen(x), Range:="",_ ConfirmConversions:=False,Link:=False, Attachment:=False
來源:鄭曉博老師微博https://weibo.com/ttarticle/p/show?id=2309404191441134950480