Solved

Merging Word documents into a single document

Posted on 2013-11-11
6
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
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 shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

733 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