views:

587

answers:

3

I have huge number of Word files I need to merge (join) into one file, and will be time consuming to use the Word merger (one by one). Have you experienced any tool that can handle this job?

A: 

Have you tried using the Word COM api? You can automate lots of things - maybe you can automate a merge.

Do you really need to do an actual merge, or do you want to join the files together. The two things are quite different.

Merging is used when you have two versions of an original file with (potentially conflicting) changes. I can't really see how you would have a "huge number" of files that you needed to merge all together. This would be an absolute nightmare of conflicts. Do you mean to merge sets of them into individual files?

Joining would be when you want to concatenate them one after the other. This would be a lot easier to do. This is quite possible using the COM api.

1800 INFORMATION
+1  A: 

I came across a post by Graham Skan a while back. It might get you started:

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
Mitch Wheat
Mitch, sorry for the double edit. SO's highlighting module is notoriously broken for VBA/VBS. +1 to get you off the end of the list.
Tomalak
no problem. I'm giving you +1 because it's a better bit of code!
Mitch Wheat
How did you fix the highlighting?
Mitch Wheat
The highlighter assumes a string starts at an apostrophe. So VBA line comments are seen as a "never ending string", I close them with another apostrophe. Same for strings ending with \". I "close" them by appending a comment that goes like this: ' some explaining text ' "
Tomalak
+3  A: 
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"

I have one question - why do you want do do such a thing (with a "huge number" of documents, at least)?

Tomalak
I have different SRS for one huge application and the client is requesting I join them in one master file, that's why.
whiz
I see. Does it work?
Tomalak
didn't try yet. will let u know.
whiz
Can you please advise where I can get the Document dll from?
whiz
This is VBA code, run it inside a Word document (use ALT-F11 to open the VBA IDE). You don't need any extra DLL for that.
Tomalak
Hi Tomalak. Thanks for your replies. I tried your code and seems the MasterDocument.Range.InsertFile overrides each document with the new one and does not append. Any advice?I had also to add MasterDocument.save to make it work. Is this correct?
whiz
Yes, it is. I'll correct my code sample so these errors are accounted for.
Tomalak
whiz