5

我有大量的 Word 文件需要合并(加入)到一个文件中,并且使用 Word 合并(一个一个)会很耗时。您是否体验过任何可以处理这项工作的工具?

4

3 回答 3

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"

我有一个问题——你为什么要做这样的事情(至少有“大量”文件)?

于 2008-11-10T09:55:54.153 回答
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
于 2008-11-10T09:46:57.290 回答
0

您是否尝试过使用 Word COM api?你可以自动化很多事情——也许你可以自动化合并。

你真的需要做一个实际的合并,还是你想把文件连接在一起。这两件事是完全不同的。

当您有两个版本的原始文件具有(可能冲突的)更改时,将使用合并。我真的看不出您将如何拥有需要将所有文件合并在一起的“大量”文件。这绝对是一场冲突的噩梦。您的意思是将它们的集合合并到单个文件中吗?

加入将是当您想要一个接一个地连接它们时。这会容易得多。这很可能使用 COM api。

于 2008-11-10T09:39:24.110 回答