{"id":55373,"date":"2008-06-12T02:06:00","date_gmt":"2008-06-12T02:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/12\/hey-scripting-guy-how-can-i-get-a-list-of-all-the-styles-in-a-word-document\/"},"modified":"2008-06-12T02:06:00","modified_gmt":"2008-06-12T02:06:00","slug":"hey-scripting-guy-how-can-i-get-a-list-of-all-the-styles-in-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-get-a-list-of-all-the-styles-in-a-word-document\/","title":{"rendered":"Hey, Scripting Guy! How Can I Get a List of All the Styles in a Word Document?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! Is it possible to get a list of all the styles available in a Word document?<br \/>&#8212; RK<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>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\u2019re getting ready to fly back home. That wasn\u2019t originally the plan; originally we planned to spend as much time \u2013 and as much of Microsoft\u2019s money \u2013 as we could. But that was before we found out that the Scripting Son\u2019s high school graduation will be held this Friday. Once he learned that, the Scripting Guy who writes this column decided he\u2019d better leave Orlando as soon as possible; that way he\u2019d 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 <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/monday.mspx\"><b>Day 1<\/b><\/a> of TechEd 2007. It wasn\u2019t 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.<\/p>\n<table class=\"dataTable\" id=\"EBD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. And what about explaining to the Scripting Son\u2019s <i>mom<\/i> that he was going to miss graduation? He doesn\u2019t even want to <i>think<\/i> about that.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Of course, even though we didn\u2019t get to stay very long we still have some fond memories of Orlando and of TechEd. What\u2019s that? What\u2019s our <i>fondest<\/i> memory of Orlando and TechEd? That\u2019s 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\u2019t get a chance to post this on <a href=\"http:\/\/flickr.com\/\" target=\"_blank\"><b>Flickr<\/b><\/a> just yet, but here\u2019s a snapshot of the script we\u2019re talking about:<\/p>\n<pre class=\"codeSample\">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: \" &amp; objStyle.NameLocal    Wscript.Echo \"Font Name: \" &amp; objStyle.Font.Name    Wscript.Echo \"Font Size: \" &amp; objStyle.Font.Size    Wscript.Echo \"Bold: \" &amp; objStyle.Font.Bold    Wscript.Echo \"Italic: \" &amp; objStyle.Font.Italic    Wscript.EchoNext<\/pre>\n<p>You can see why we thought this was so exciting, can\u2019t you?<\/p>\n<p>No doubt you\u2019re just <i>dying<\/i> 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 <b>Word.Application<\/b> object and then set the <b>Visible<\/b> property to True; that gave us a running instance of Word that we could see on screen. Once we <i>had<\/i> a running instance of Word we could use the <b>Open<\/b> method to open the file C:\\Scripts\\Test.doc. Here, here\u2019s a picture of the Open method in action:<\/p>\n<pre class=\"codeSample\">Set objDoc = objWord.Documents.Open(\"C:\\Scripts\\Test.doc\")<\/pre>\n<p>Believe it or not, the rest was easy. Each <b>Document<\/b> object in Word includes a property named <b>Styles<\/b> 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:<\/p>\n<pre class=\"codeSample\">For Each objStyle in objDoc.Styles    Wscript.Echo \"Style Name: \" &amp; objStyle.NameLocal    Wscript.Echo \"Font Name: \" &amp; objStyle.Font.Name    Wscript.Echo \"Font Size: \" &amp; objStyle.Font.Size    Wscript.Echo \"Bold: \" &amp; objStyle.Font.Bold    Wscript.Echo \"Italic: \" &amp; objStyle.Font.Italic    Wscript.EchoNext<\/pre>\n<p>We should probably note that we\u2019re 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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa223718(office.11).aspx\" target=\"_blank\"><b>Microsoft Word VBA Language Reference<\/b><\/a>.<\/p>\n<p>And, before we forget, here\u2019s the kind of output you can expect to see:<\/p>\n<pre class=\"codeSample\">Style Name: SignatureFont Name: Times New RomanFont Size: 12Bold: 0Italic: 0<\/pre>\n<p>Pretty cool, if we do say so ourselves.<\/p>\n<p>Well, that\u2019s all the time we have for today, RK. We hope that \u2013 what\u2019s that? Did someone just say \u201crip off?\u201d Oh, that hurts, it really does; you\u2019ve cut us to the quick. OK, sure, today\u2019s column <i>is<\/i> a little shorter than usual, but that\u2019s not our fault; as you just saw, it\u2019s just really, <i>really<\/i> easy to return style information for a Word document. We didn\u2019t write very much for one simple reason: there wasn\u2019t very much to write about. <\/p>\n<p>And did we mention that we have a plane to catch? And that the Scripting Son graduates on Friday?<\/p>\n<table class=\"dataTable\" id=\"EOF\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Is the Scripting Dad excited about the Scripting Son graduating? Well, to be honest, he has mixed emotions. After all, this means that he\u2019ll soon have a son in college, and he\u2019s always thought that people who had college-age children must be really, really, <i>really <\/i>old. Apparently he was wrong about that.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>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? <\/p>\n<p>Deal. Here\u2019s a script that can add a user-defined style to a Word document:<\/p>\n<pre class=\"codeSample\">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<\/pre>\n<p>Let\u2019s see what we have here. To begin with, we define a pair of constants, wdStyleTypeCharacter and wdAlignParagraphCenter. We\u2019ll use wdStyleTypeCharacter to define our new style as a character style, and we\u2019ll use wdAlignParagraphCenter to indicate that we want any type formatted with this style to be centered on screen. (For more information see the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa223718(office.11).aspx\" target=\"_blank\"><b>VBA Language Reference<\/b><\/a>.) <\/p>\n<p>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.<\/p>\n<p>And yes, you\u2019ve 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\u2019t want to bore you (hey, who said, \u201cToo late\u201d?), so let\u2019s now show you something brand-new:<\/p>\n<pre class=\"codeSample\">Set objMyStyle = objDoc.Styles.Add(\"My Style\", wdStyleTypeCharacter)<\/pre>\n<p>What we\u2019re doing here is using the <b>Add<\/b> 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.<\/p>\n<p>Of course, at the moment this new style will be anything but stylish; that\u2019s because we didn\u2019t define any properties for the style, meaning that it simply inherits the properties of the default character style in Word. But that\u2019s OK; the last three lines of code in the script are designed to rectify that:<\/p>\n<pre class=\"codeSample\">objMyStyle.Font.Name = \"Arial\"objMyStyle.Font.Size = 48objMyStyle.ParagraphFormat.Alignment = wdAlignParagraphCenter<\/pre>\n<p>Needless to say, there\u2019s nothing very fancy going on here. In lines 1 and 2 we\u2019re assigning font properties to the style; in particular, we\u2019re setting the font type (<b>Name<\/b>) to Arial and the <b>Size<\/b> to 48-point. In the last line we\u2019re setting the paragraph alignment to centered; that\u2019s done by referencing the style\u2019s <b>ParagraphFormat<\/b> object and then setting the <b>Alignment<\/b> property. Needless to say, we could make this style as simple \u2013 or as fancy \u2013 as we desire.<\/p>\n<p>And now we really <i>must<\/i> head off for the airport. Incidentally, do you want to know something interesting about the Orlando International Airport? How \u2018bout that? So do we. Listen, if you ever hear anything interesting could you let us know? Thanks!<\/p>\n<table class=\"dataTable\" id=\"EEH\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>True story<\/b>. 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.<\/p>\n<p>Traveling can be so much fun.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to get a list of all the styles available in a Word document?&#8212; RK 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\u2019re [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[84,49,3,5],"class_list":["post-55373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is it possible to get a list of all the styles available in a Word document?&#8212; RK 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\u2019re [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=55373"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55373\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=55373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}