Solved

Merging Word documents into a single document

Posted on 2013-11-11
6
638 Views
Last Modified: 2014-01-06
THis question is a follow up question to http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_28291498.html

How can I quickly merge all the Word documents in a folder into a single document, and add a page break between songs?  I would like the songs added in alphabetical order as well.

Thanks!
Lantau-Songbook.zip
0
Comment
Question by:tomfolinsbee
  • 4
  • 2
6 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39642413
Perfectly feasible though the definition of quickly may be the issue however I suggest we get the previous question resolved before you take this one any further to avoid conflict

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39650440
Noting the previous question request to keep column widths this is not as easy as i thought it would be ... do you still want the individual files to keep thei column widths ... because on my first pass some of the data  is lost to the right of the document.

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009
'
' NAME: 
'
' AUTHOR: Jaguar Land Rover , Jaguar Land Rover
' DATE  : 15/11/2013
'
' COMMENT: 
'
'==========================================================================

Dim wdApp
Dim strErr
Dim tbl
Dim doc
Dim startFlag

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\DeleteMe\Test1")
Set Files = folder.Files

Set wdApp = CreateObject("Word.application")
wdApp.visible = false
Set masterDoc = wdApp.Documents.add
startFlag = false
wrdPageBreak = 7
wrdCollapseEnd = 0
For Each fil In Files
    If Right(LCase(fil), 5) = ".docx" OR Right(LCase(fil), 5) = ".docm" OR Right(LCase(fil), 4) = ".doc" Then
        Set doc = wdApp.Documents.Open(fil.Path)
	    Set RNG = masterdoc.Range
	    RNG.Collapse wrdCollapseEnd
		If startFlag Then
		    RNG.InsertBreak wrdPageBreak
		Else
			startFlag = true
		End If
	    RNG.InsertAfter doc.range.text
		doc.close
    End If
Next
wdApp.visible = True
    WScript.Echo "Finished"

Open in new window


Chris
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39650536
Improved answer, inherits the column settings from each document:

'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009
'
' NAME: 
'
' Author:Chris Bottomley
' DATE  : 15/11/2013
'
' COMMENT: 
'
'==========================================================================

Dim wdApp
Dim strErr
Dim tbl
Dim doc
Dim startFlag

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\DeleteMe\Test1")
Set Files = folder.Files

Set wdApp = CreateObject("Word.application")
wdApp.visible = false
startFlag = false
wrdPageBreak = 7
wrdCollapseEnd = 0
For Each fil In Files
    If Right(LCase(fil), 5) = ".docx" OR Right(LCase(fil), 5) = ".docm" OR Right(LCase(fil), 4) = ".doc" Then
		If startFlag Then
		    masterdoc.Bookmarks("\EndOfDoc").Range.InsertBreak wrdPageBreak
		Else
			Set masterDoc = wdApp.Documents.add(fil.path)
			startFlag = true
		End If
	    masterdoc.Bookmarks("\EndOfDoc").Range.InsertFile fil.path
    End If
Next
wdApp.visible = True
    WScript.Echo "Finished"

Open in new window


Chris
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:tomfolinsbee
ID: 39659152
Hi Chris,

I saved the script in notepad, and saved it, but it didn't turn in a .vbs file. So I copied it to an existing .vbs file, saved it, and ran it. I got a "finished" message, but I couldn't find any merged document. Where's the destination folder? Assume its supposed to be the same as the one where the script is?

Cheers,

Tom
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39660070
The new document is opened but not saved so you need to save the new document before closing it

Chris
0
 

Author Closing Comment

by:tomfolinsbee
ID: 39759334
Apologies for the late reply, thanks for the Solution!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question