2008-11-10 72 views
4

我有大量的Word文件需要合併(連接)到一個文件中,並且使用Word合併(一個接一個)會很耗時。你有沒有經歷過任何可以處理這項工作的工具?合併MS Word文件的工具

回答

4
Sub MergeAllDocuments(AllDocumentsPath as String, MasterDocumentPath as String) 
    Dim MasterDocument As Document 

    Set MasterDocument = Documents.Open(FileName:=MasterDocumentPath) 

    TheDocumentPath = Dir(AllDocumentsPath , vbNormal) 
    While TheDocumentPath <> "" 
    ' Append the next doc to the end of the master doc. (The 
    ' special "\EndOfDoc" bookmark is always available!) 
    MasterDocument.Bookmarks("\EndOfDoc").Range.InsertFile TheDocumentPath 
    TheDocumentPath = Dir 
    Wend 

    MasterDocument.Save 
End Sub 

MergeAllDocuments "C:\MySeparateDocuments\*.doc", "C:\MasterDocument.doc" 

我有一個問題 - 爲什麼你要不要做這樣的事情(與文檔的「數量巨大」,至少)?

0

您是否嘗試過使用Word COM API?你可以自動化很多事情 - 也許你可以自動化合並。

你真的需要做一個實際的合併,或者你想一起加入文件。這兩件事情完全不同。

當您有兩個版本的原始文件(可能存在衝突)更改時使用合併。我無法真正看到你將如何擁有需要將所有文件合併在一起的「大量」文件。這將是一場絕對的衝突噩夢。你的意思是把它們集合成單個文件嗎?

加入是當你想連接他們一個接一個。這會容易得多。這很可能使用COM API。

2

我偶然發現了Graham Skan的帖子。它可能讓你開始:

Sub InsertFiles() 
    Dim strFileName As String 
    Dim rng As Range 
    Dim Doc As Document 
    Const strPath = "C:\Documents and Settings\Graham Skan\My Documents\Allwork\" 'adjust as necessary '" 

    Set Doc = Documents.Add 
    strFileName = Dir$(strPath & "\*.doc") 
    Do 
     Set rng = Doc.Bookmarks("\EndOfDoc").Range 
     If rng.End > 0 Then 'section break not necessary before first document.' 
      rng.InsertBreak wdSectionBreakNextPage 
      rng.Collapse wdCollapseEnd 
     End If 
     rng.InsertFile strPath & "\" & strFileName 
     strFileName = Dir$() 
    Loop Until strFileName = "" 
End Sub 
+0

米奇,抱歉雙編輯。 SO的突出顯示模塊因VBA/VBS而出名。 +1將您排除在列表的末尾。 – Tomalak 2008-11-10 10:05:41

+0

沒問題。我給你+1,因爲它是一個更好的代碼! – 2008-11-10 10:11:29