Hey, Scripting Guy! We have a bunch of Word documents that, for some reason, have been formatted using nothing but lowercase letters. We know exactly how we want to reformat these documents: all the headings (which are in 14-point type) should be in title case and all the other paragraphs should be in sentence case. Of course, it’s pretty tedious to reformat these documents by hand; some of them are quite long. How can I use a script to change the paragraph case?
— JH
Hey, JH. We don’t know if you pay much attention to public education here in the state of Washington (heaven knows that the students don’t seem to pay much attention to public education here in the state of Washington) but the state recently – and proudly – announced that 83.6 percent of the students in the class of 2008 had passed the WASL (Washington Assessment of Student Learning) and were on target to graduate. Congratulations guys; the champagne’s on us!
Of course, that 83.6 percent doesn’t include students who have already dropped out of school, and it doesn’t include students who are still in school but have fallen behind on their credits and will not graduate on time. Oh, and it doesn’t take into account the fact that over one-third of the students who took the WASL actually failed the math portion and thus, according to state law, are not allowed to graduate. (And yet still they couldn’t figure out how to get a 100% graduation rate.)
The Scripting Guys’ favorite part of all this is the math portion of the WASL. More than one-third of the students who took the math portion of the WASL failed. That’s nothing short of disastrous, especially in light of the fact that you have to pass all three parts of the WASL, including math, in order to graduate. So how do you suppose we dealt with an educational disaster of those proportions?
That’s easy: we simply changed the law and said, “Never mind; turns out that you can flunk the math portion of the WASL and graduate after all.” Problem solved.
Besides, who uses math in this day and age?
Judging by the number of bankruptcies, loan defaults, and foreclosures that occur each and every day, no one.
In the interest of full disclosure, we should note that none of the Scripting Guys passed the math portion of the WASL, either, although that’s due, at least in part, to the fact that they didn’t have the WASL back in those days. (Come to think of it, they didn’t have paper or pencils back in those days either.) However, the state legislature has decided to give us a break, provided we could write a script that can reformat paragraph case in a Microsoft Word document. As it turns out, we couldn’t. Fortunately for us, however, and in the spirit of the modern-day student, we were able to steal one off the Internet instead:
Const wdTitleWord = 2 Const wdTitleSentence = 4Set objWord = CreateObject(“Word.Application”) objWord.Visible = True
Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)
Set colParagraphs = objDoc.Paragraphs
For Each objParagraph in colParagraphs If objParagraph.Range.Font.Size = 14 Then objParagraph.Range.Case = wdTitleWord Else objParagraph.Range.Case = wdTitleSentence End If Next
As you can see, we (well, OK, whoever actually wrote this script) start out by defining two constants, wdTitleWord (with a value of 2) and wdTitleSentence (with a value of 4). What are we going to do with these two constants? Just a second; we need to start up Internet Explorer.
Um, as we were saying, we’ll use wdTitleWord to apply the title case to all the headings in the document; that’s going to capitalize the first letter of each word in those headings. Meanwhile, we’ll use wdTitleSentence to apply the sentence case to all the other paragraphs in the document. With sentence casing, the first word in a sentence (determined by closing punctuation marks like . or ! or ?) will be capitalized; all the other words in the sentence will remain lowercase.
Admittedly, this is not a foolproof solution. (But don’t blame us; we didn’t write it!) For example, suppose you have the following sentence in your document:
i live in new york city.
After we apply sentence casing, that same sentence will look like this:
I live in new york city.
That’s better, but it’s not perfect; after all, that should be New York City, not new york city. But this is about the best we can hope for.
After we define our constants we create an instance of the Word.Application object and set the Visible property to True; that gives us a running instance of Microsoft Word that we can see on screen. We then use this line of code (and the Open method) to open the document C:\Scripts\Test.doc:
Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)
In case you’re wondering, that document looks like this:
See all those lowercase letters everywhere you look? That’s what we need to fix.
In order to do that, our next step is to use this line of code to return a collection of all the paragraphs in our document:
Set colParagraphs = objDoc.Paragraphs
Once we’ve done that we then use this line of code to set up a For Each loop to walk us through each paragraph in that collection:
For Each objParagraph in colParagraphs
Still with us? Good. Because now we’re ready for the fun part.
Inside the loop, we use the following line of code to determine whether the paragraph (or, more correctly, the paragraph’s Range) uses a font size of 14 points:
If objParagraph.Range.Font.Size = 14 Then
Suppose the paragraph does use a 14-point font. Well, with this particular document, that can mean only one thing: this paragraph is a heading. Therefore, we set the value of the Range object’s Case property to the constant wdTitleWord, something that applies the title case to the paragraph:
objParagraph.Range.Case = wdTitleWord
And what if the paragraph doesn’t use a 14-point font? No problem; in that case we set the value of the Case property to the constant wdTitleSentence, which applies the sentence case to the paragraph:
objParagraph.Range.Case = wdTitleSentence
And then we loop around and repeat the process with the next paragraph in the collection. By the time we’re finished looping our document should look like this:
Which is just exactly what we want it to look like.
We hope that answers your question, JH. Incidentally, if you or anyone else out there is thinking about hiring some new employees sometime next summer, well, we’d strongly recommend choosing a high school graduate from the state of Washington. Would these new graduates do the job really well? Beats us. On the other hand, these kids almost all flunked math; that means you can pay them anything you want a get away with it:
“No, $11 a day is more than fair. Do you have any idea how big a number 11 really is? That’s what we thought.”
Just something to keep in mind.
Editor’s Note: If today’s column sounded familiar to you, you’re obviously a devoted reader of the Hey, Scripting Guy! column – and much farther from senility than the Scripting Guy who writes this column. |
0 comments