Hey, Scripting Guy! How Can I Get a List of All the Styles in a Word Document?


Hey, Scripting Guy! Question

Hey, Scripting Guy! Is it possible to get a list of all the styles available in a Word document?
— RK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RK. Believe it or not, TechEd Orlando is over, at least for the Scripting Guys. Admittedly, it was a somewhat abbreviated trip: we flew in, presented a couple of instructor-led labs, and now we’re getting ready to fly back home. That wasn’t originally the plan; originally we planned to spend as much time – and as much of Microsoft’s money – as we could. But that was before we found out that the Scripting Son’s high school graduation will be held this Friday. Once he learned that, the Scripting Guy who writes this column decided he’d better leave Orlando as soon as possible; that way he’d have plenty of leeway in case anything went wrong. After all, last year his Sunday night flight from Seattle to Orlando was cancelled, causing him to miss Day 1 of TechEd 2007. It wasn’t a lot of fun explaining to the Scripting Editor that he was going to miss Day 1 of TechEd; it would be even less fun to explain to the Scripting Son that he was going to miss graduation.

Note. And what about explaining to the Scripting Son’s mom that he was going to miss graduation? He doesn’t even want to think about that.

Of course, even though we didn’t get to stay very long we still have some fond memories of Orlando and of TechEd. What’s that? What’s our fondest memory of Orlando and TechEd? That’s an easy one: it has to be coming up with a script that retrieves information about all the styles available in a Microsoft Word document. We didn’t get a chance to post this on Flickr just yet, but here’s a snapshot of the script we’re talking about:

Set objWord = CreateObject("Word.Application")objWord.Visible = TrueSet objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")For Each objStyle in objDoc.Styles    Wscript.Echo "Style Name: " & objStyle.NameLocal    Wscript.Echo "Font Name: " & objStyle.Font.Name    Wscript.Echo "Font Size: " & objStyle.Font.Size    Wscript.Echo "Bold: " & objStyle.Font.Bold    Wscript.Echo "Italic: " & objStyle.Font.Italic    Wscript.EchoNext

You can see why we thought this was so exciting, can’t you?

No doubt you’re just dying to hear about our trip to Orlando, and how we were able to come up with a script that can list all the styles found in a Word document. Well, to begin with, we created an instance of the Word.Application object and then set the Visible property to True; that gave us a running instance of Word that we could see on screen. Once we had a running instance of Word we could use the Open method to open the file C:\Scripts\Test.doc. Here, here’s a picture of the Open method in action:

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")

Believe it or not, the rest was easy. Each Document object in Word includes a property named Styles that returns a collection of all the styles in that document. Want information about each style? Then simply set up a For Each loop that loops through all the items in the Styles collection and have at it:

For Each objStyle in objDoc.Styles    Wscript.Echo "Style Name: " & objStyle.NameLocal    Wscript.Echo "Font Name: " & objStyle.Font.Name    Wscript.Echo "Font Size: " & objStyle.Font.Size    Wscript.Echo "Bold: " & objStyle.Font.Bold    Wscript.Echo "Italic: " & objStyle.Font.Italic    Wscript.EchoNext

We should probably note that we’re echoing back only a few properties of each style (mostly font-related properties). Are there other style properties we could retrieve using a script? You bet there are; for more information, see the Microsoft Word VBA Language Reference.

And, before we forget, here’s the kind of output you can expect to see:

Style Name: SignatureFont Name: Times New RomanFont Size: 12Bold: 0Italic: 0

Pretty cool, if we do say so ourselves.

Well, that’s all the time we have for today, RK. We hope that – what’s that? Did someone just say “rip off?” Oh, that hurts, it really does; you’ve cut us to the quick. OK, sure, today’s column is a little shorter than usual, but that’s not our fault; as you just saw, it’s just really, really easy to return style information for a Word document. We didn’t write very much for one simple reason: there wasn’t very much to write about.

And did we mention that we have a plane to catch? And that the Scripting Son graduates on Friday?

Note. Is the Scripting Dad excited about the Scripting Son graduating? Well, to be honest, he has mixed emotions. After all, this means that he’ll soon have a son in college, and he’s always thought that people who had college-age children must be really, really, really old. Apparently he was wrong about that.

Tell you what: how about we show you a script that can add a new, user-defined style to Microsoft Word? We show you that script, and then you let us take off for the airport? Deal?

Deal. Here’s a script that can add a user-defined style to a Word document:

Const wdStyleTypeCharacter = 1Const wdAlignParagraphCenter = 1Set objWord = CreateObject("Word.Application")objWord.Visible = TrueSet objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")Set objMyStyle = objDoc.Styles.Add("My Style", wdStyleTypeCharacter)objMyStyle.Font.Name = "Arial"objMyStyle.Font.Size = 48objMyStyle.ParagraphFormat.Alignment = wdAlignParagraphCenter

Let’s see what we have here. To begin with, we define a pair of constants, wdStyleTypeCharacter and wdAlignParagraphCenter. We’ll use wdStyleTypeCharacter to define our new style as a character style, and we’ll use wdAlignParagraphCenter to indicate that we want any type formatted with this style to be centered on screen. (For more information see the VBA Language Reference.)

After defining the two constants we create an instance of the Word.Application object, set the Visible property to True, then open the file C:\Scripts\Test.doc.

And yes, you’ve seen all that before. (When? Just a few minutes ago. Remember, when we showed you how to return information about all the styles in Test.doc?) We don’t want to bore you (hey, who said, “Too late”?), so let’s now show you something brand-new:

Set objMyStyle = objDoc.Styles.Add("My Style", wdStyleTypeCharacter)

What we’re doing here is using the Add method (which belongs to the Style collection) to add a new style to our document. To do so we simply pass the Add method two parameters: the name of our new style (My Style) and the style type (wdStyleTypeCharacter). At that point, our document will have a new style named My Style.

Of course, at the moment this new style will be anything but stylish; that’s because we didn’t define any properties for the style, meaning that it simply inherits the properties of the default character style in Word. But that’s OK; the last three lines of code in the script are designed to rectify that:

objMyStyle.Font.Name = "Arial"objMyStyle.Font.Size = 48objMyStyle.ParagraphFormat.Alignment = wdAlignParagraphCenter

Needless to say, there’s nothing very fancy going on here. In lines 1 and 2 we’re assigning font properties to the style; in particular, we’re setting the font type (Name) to Arial and the Size to 48-point. In the last line we’re setting the paragraph alignment to centered; that’s done by referencing the style’s ParagraphFormat object and then setting the Alignment property. Needless to say, we could make this style as simple – or as fancy – as we desire.

And now we really must head off for the airport. Incidentally, do you want to know something interesting about the Orlando International Airport? How ‘bout that? So do we. Listen, if you ever hear anything interesting could you let us know? Thanks!

True story. Last year at the Orlando Airport the Scripting Guys were standing in line waiting to check in. A fellow Microsoft employee realized that no one was using the self-check-in kiosks, so he walked up to one of the machines intending to check-in. Immediately he was set upon by another passenger, a man who started yelling and screaming and demanding that the Microsoft employee get back in line and quit taking cuts. After arguing for several minutes the Microsoft employee finally gave up and returned to his spot in line. The moment he did so the other passenger got out of line, walked past everyone else and immediately used the kiosk to check himself in.

Traveling can be so much fun.


Discussion is closed.

Feedback usabilla icon