How Can I Insert Multiple Files Into a Word Document, Putting a Page Break Between Each File?

Hey, Scripting Guy! Question

Hey, Scripting Guy! On May 3rd you showed us how to merge multiple files in Microsoft Word. I’d like to do that, but put a page break between each file.

— CE

Spacer Hey, Scripting Guy! Answer Script Center

Hey, CE. You know, when we read your email our first thought was this: no way did we show people how to merge multiple files back on May 3rd 2005. That wasn’t because we didn’t think that the subject matter – merging multiple files into a single Microsoft Word document -sounded like the kind of topic we’d tackle in this column. Instead, it’s just that on most days we don’t do any work, and we couldn’t see why May 3rd would be an exception. But it turns out you were right: we actually did do some work on May 3rd. What were we thinking?

As you no doubt recall (for surely you all memorize each and every Hey, Scripting Guy! column), in that initial column we used a script to import all the files found in the folder C:\Scripts\Archive into a Microsoft Word document. That was fine, except all we did was type a paragraph return between the end of one file and the beginning of the next. Needless to say, if it’s important to know where one file leaves off and the next one begins, one measly little paragraph return probably won’t be of much use. You’re right, CE: inserting a page break between the files is definitely a step in the right direction. And here’s how you do that:

For the most part we’ve simply replicated our script for inserting multiple files; therefore, we won’t go over the code in line-by-line detail. (If you need that information, take a look at the original column.) Instead, we’ll focus on the two changes we made.

To begin with, the script now starts off by defining a constant named wdPageBreak and setting the value to 7. We’ll use this constant to tell Word to insert a page break when we call the InsertBreak method. (As you probably know, Word allows you to insert other kinds of breaks, including column breaks, text-wrapping breaks, and several kinds of section breaks.)

We then have a bunch of code that binds to the WMI service on the local computer, creates a new Microsoft Word document, and then uses an Associators Of query to retrieve a collection of all the files found in the C:\Scripts\Archive folder. Once we have that collection we set up a For Each loop to walk through the collection, using the InsertFile method to insert each and every file.

Here’s the second place where the new script differs from the original. In our initial script, we called the InsertFile method, then used TypeParagraph() to simulate pressing the ENTER key on the keyboard. This time around, our For Each loop looks like this:

As you can see, after inserting the first file we call the InsertBreak method, specifying that we want to insert a page break. It’s only after inserting this page break that we loop around and insert the next file in the collection. The net effect: we end up with page breaks between each of the files we imported into our document.

Yes, much better. And seeing as how we’ve already done some work today, we might as well do a little bit more. Here’s a revised script that not only inserts page breaks between each file, but also types in the file path after each page break, formatting that path in 14-point Arial. (The rest of the text is formatted in 10-point Arial.) Here’s what this little bonus script looks like:

Note. For more information about formatting text in a Word document, take a look at the Microsoft Word articles that have appeared in the weekly Office Space column.

You know, that was kind of fun. Maybe, just for a change of pace, we’ll do some work every May 3rd from now on.

Nah ….


No Comment.