Solved

Merging Word documents into a single document

Posted on 2013-11-11
6
617 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now