Solved

Merging Word documents into a single document

Posted on 2013-11-11
6
596 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:tomfolinsbee
Comment Utility
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
Comment Utility
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
Comment Utility
Apologies for the late reply, thanks for the Solution!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

763 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

10 Experts available now in Live!

Get 1:1 Help Now