Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I save (to a separate file) all the paragraphs in a Word document that are formatted using a particular style, like Heading 1?

-- RN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RN. You know, when we first read this question we thought, “Interesting. But why would you ever want to do something like that?” And then one of the Scripting Guys remembered the many…happy…hours he spent one week going through Word documents and manually copying and pasting all the headings, to create a master list of topics. Once we had remembered that, we thought, “You know, it would have been really nice to have a had a script like this back then. But because we had to suffer through that chore without a script, why shouldn’t everyone have to suffer through that chore without a script?”

But then someone else made a good point: while the Scripting Guys probably deserve to suffer, most script writers don’t. Therefore, we decided to come up with a script that can pull out selected paragraphs from a Word document. Here’s a simple little script that checks each paragraph to see if it uses the Heading 1 style. If the paragraph uses that style, the script leaves it alone; if the paragraph doesn’t use that style, the script deletes it.

Why? Well, this seemed like the easiest way to handle the issue: instead of copying Heading 1 paragraphs from one document to another, we just delete everything that isn’t a Heading 1 from the document. When we’re all done, we then use the SaveAs method to save the file under a separate file name. The net result? We still have the original document, plus we have a new document that contains only the paragraphs formatted as Heading 1.

Here’s the script:

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Set objSelection = objWord.Selection
Set colParagraphs = objDoc.Paragraphs

For Each objParagraph in colParagraphs
    If objParagraph.Style <> "Heading 1" Then
        objParagraph.Range.Select
        objSelection.Cut
    End If    
Next

objDoc.SaveAs("C:\Scripts\Headings.doc")
objWord.Quit

We begin by creating an instance of the Word.Application object and then set the Visible property to True; we do that just so everything that happens will be visible on screen. We then use the Open method to open the file C:\Scripts\Test.doc.

Note. Needless to say, if you decide to try this script your results will be far more interesting if Test.doc actually includes a few paragraphs that use the Heading 1 style.

We create an instance of the Word Selection object and then use this line of code to create an object reference to our document’s Paragraphs collection:

Set colParagraphs = objDoc.Paragraphs

As you probably guessed, the Paragraphs collection contains all the paragraphs found in our document. Next we set up a For Each loop to loop through this collection. For each paragraph in the collection (and thus for each paragraph in the document) we use this line of code to see if the paragraph uses the Heading 1 style:

If objParagraph.Style <> "Heading 1" Then

If the paragraph does use the Heading 1 style, we simply loop back around and check the next item in the collection. But what if the paragraph doesn’t have the Heading 1 style (that if, what if our If statement is true)? In that case we use these two lines of code to select the paragraph and then cut it from the document:

objParagraph.Range.Select
objSelection.Cut

After we’ve looped through the entire collection - and deleted all the paragraphs that don’t use the Heading 1 style - we’ll be left with nothing but Heading 1 paragraphs. At that point all we have to do is call the SaveAs method and save the document under a new name (in this case, C:\Scripts\Headings.doc) and then exit Word. And that’s what we do with these two lines of code:

objDoc.SaveAs("C:\Scripts\Headings.doc")
objWord.Quit

And, yes, we do feel better for having shared this with the world. (Although we still would have liked to make you suffer a little. But just a little, mind you.)

Incidentally, if this whets your appetite for Microsoft Office scripting, be sure and check out the Office Space column, published every Tuesday and Thursday.