: ) wonderful world ( :

the metasyntactic variable

concatenate or merge docx Word files dot vbs

leave a comment »

In case you have generated Word files which are parts of a future “master” document and also you want to automate the process of assembling them together by writing some sort of build script, the following snippet is probably useful. Generating documents is good to avoid redundancy. In case you have to have a document in which parts can be computed based on other parts (with also the miserable contraint on document format and production environment) , it’s good to consider splitting the document, generating the parts and assembling them at the end. And now, the code:

C:\Users\grault\Desktop\a>dir/b
a1.docx
a2.docx
build.vbs

C:\Users\grault\Desktop\a>type build.vbs
Set objWord = CreateObject("Word.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objDoc = objWord.Documents.Add()

objDoc.ActiveWindow.View.Type = 2 'wdOutlineView
For I = 1 To WScript.Arguments.Count - 1
   objDoc.Subdocuments.AddFromFile _
     (objFSO.GetAbsolutePathName(WScript.Arguments.Item(I)))
Next

objDoc.SaveAs(objFSO.GetAbsolutePathName(WScript.Arguments.Item(0)))
objWord.Quit
C:\Users\grault\Desktop\a>build.vbs out.docx a1.docx a2.docx

C:\Users\grault\Desktop\a>dir/b
a1.docx
a2.docx
build.vbs
out.docx

C:\Users\grault\Desktop\a>

Written by grault

May 15, 2009 - 9:46 am at May 15, 2009 - 9:46 am

Posted in notes, script, windowns

Leave a Reply