Hey, Scripting Guy! How can I clear all the formatting from a Microsoft Word document?
— TF
Hey, TF. After a much-deserved holiday hiatus we are back, with the first official Hey, Scripting Guy! column for the year 2007. And yes, we know: technically we did have a column yesterday. As it turns out, however, that column was actually written a few weeks ago, before the Scripting Guy who writes this column left on vacation. Because of that, we don’t really consider it to be the first official column for the year 2007. If you looked at yesterday’s column we ask that you forget everything you read there.
And no, for most of you that won’t be much of a problem, will it?
Seeing as how this is the first official column for the year 2007, do we have plans to make today’s article extra-special and worthwhile? You bet we do; unfortunately, though, none of the other Scripting Guys wrote an extra-special , worthwhile article while the Scripting Guy who writes this column was away. Therefore, instead of anything extra-special you’re getting a simple little script that we scraped together at the last possible minute. Which, if nothing else, means that you don’t have anything to worry about: Hey, Scripting Guy! is still the same as it ever was!
Note. Speaking of which, did anyone watch the Fiesta Bowl game between Boise State and Oklahoma? Holy smokes! The stupid “Bowl Championship Series” aside, you simply have to love college football. And yes, you do have to love it. That’s just the way it goes. |
Now, where were we? Ah, yes: a script that removes all the formatting from a Microsoft Word document:
Set objWord = CreateObject(“Word.Application”) objWord.Visible = TrueSet objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)
objDoc.Select
Set objSelection = objWord.Selection objSelection.ClearFormatting
You can see why we chose this topic on our first day back to work: there really isn’t much to this thing at all. We start off by creating an instance of the ever-popular Word.Application object, then set the Visible property to True; that gives us a running instance of Microsoft Word that we can see on screen. As soon as Word is up and running we then use the Open method to open the document C:\Scripts\Test.doc:
Set objDoc = objWord.Documents.Open(“C:\Scripts\Test.doc”)
As you might expect, Test.doc is a document absolutely rife with fancy formatting:
Remember, we did say fancy formatting, not beautiful formatting.
As it turns out, Microsoft Word includes a built-in command that enables you to remove all the formatting applied to the selected text. What does that mean for us? For one thing, that means that, in order to remove the formatting from the entire document we first need to select the entire document. That’s something we can do by calling the Document object’s Select method, which – when called without any additional parameters – selects the entire document:
objDoc.Select
After we’ve selected all the text we just need to do two more things: create an instance of Word’s Selection object, then call that object’s ClearFormatting method. That requires just two additional lines of code:
Set objSelection = objWord.Selection objSelection.ClearFormatting
So what’s the net result? This is the net result:
Not bad for the first day back, if we do say so ourselves.
Speaking of the first day back, that’s a good question: is the Scripting Guy who writes this column excited about coming back to work after three weeks of lounging around watching football and basketball? Let’s put it this way: he’s every bit as thrilled about and dedicated to his job as he was before he went on vacation.
We’ll let you figure out what that means. Like they say, we report; you decide.
0 comments