Home > Posts > WSH script (VBScript) to concatenate multiple Word files

WSH script (VBScript) to concatenate multiple Word files

1) In a folder you wish (say onto the desktop one) create a subfolder named whatever you wish and add multiple .DOC (or also .TXT etc.) files in it, named alphabetically in the order you wish them to be concatenated (say add a numeric prefix to their name)

2) At that same folder (say the desktop one) create a file named DOCmerge.vbs (or whatever filename you wish with a .vbs [=VBScript] file extension) and add the following content in it, then drag-drop the folder you wish to merge onto the DOCmerge.vbs file in order to get a Word file with the same name as the folder, but with the suffix "_CONCATENATED".

‘DOCmerge – merges several Word documents (.DOC files) together

‘Author: George Birbilis (birbilis@kagi.com) / Zoomicon (www.zoomicon.com)

‘Version: 20080325

‘On Error Resume Next

const ONLY_DOC = false ‘set to true to allow concatenation only of .DOC files (not .TXT etc.)

const MSG_SYNTAX = "Drop a folder with Word (.DOC) documents onto this icon to produce a Word file with the concatenation of these documents"

const MSG_FINISHED = "" ‘"Finished"


const wdPasteDefault = 0

const wdPageBreak = 7


dim word, fs, folderpath, outdocname, folder, outdoc


Sub ParseCommandLine

 Dim args

 Set args = wscript.arguments

 if args.count = 0 then

  msgbox MSG_SYNTAX


 end if

 folderpath = args.item(0)

 outdocname = folderpath + TXT_SUFFIX

End Sub


Sub StartServers

 ‘– Start Word

 Set word = CreateObject("Word.Application")

 ‘word.Visible = true

 Set fs = CreateObject("Scripting.FileSystemObject")

 Set folder = fs.GetFolder(folderpath)

End Sub


Sub DeleteOldOutput

 If fs.FileExists(outdocname) Then


 End If

End Sub


Sub ProcessFile(filename, insertBreak)

 Dim doc

 Set doc = word.Documents.Open(filename)




 if insertBreak then word.Selection.InsertBreak wdPageBreak

 ‘word.Selection.PasteAndFormat wdPasteDefault

 word.Selection.Paste ‘use this one so that it works for Word2000 too


 Set doc = Nothing

End Sub


Sub Process


 Dim f, first

 first = true

 For Each f in folder.Files

  if (not ONLY_DOC) or (ucase(right(f.path,4)) = ".DOC") then

   ‘MsgBox f.path

   if first then

    ‘Set outdoc = word.Documents.Open(f.path)

    ‘— using this code so that a temp file isn’t created in the input folder (else it processes that one too)

    Set outdoc = word.Documents.Add

    outdoc.SaveAs outdocname

    ProcessFile f.path, false


    first = False


    ProcessFile f.path, true

   end if

  end if


 If Not first Then ‘if at least one file was processed



 End If

End Sub


Sub Cleanup

 Set outdoc = Nothing


 Set word = Nothing

 Set folder = Nothing

 Set fs = Nothing


End Sub






Categories: Posts
  1. George
    2008/06/06 at 13:52

    An important note though: page layout of different sections/pages is not respected by the above script, so either fix the page/section formatting by hand (create section breaks where needed and change page layout for those sections you want to layout say horizontally [landscape] instead of vertically [portrait]). Can also try uncommenting "word.Selection.PasteAndFormat wdPasteDefault" by removing the \’ in front of it and commenting the next line instead, in case that action also copy/pastes page orientation (haven\’t tried it and find it unlikely though). Doing that change will render the script non-workable at older Word versions though. To fix this issue a total rewrite of the script is needed to enumerate all sections and copy text per section, also reading and setting the section orientation and whatever other section layout settings from the source file to the target file

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: