{"id":63953,"date":"2007-09-25T02:15:00","date_gmt":"2007-09-25T02:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/09\/25\/how-can-i-change-the-paragraph-case-in-microsoft-word\/"},"modified":"2007-09-25T02:15:00","modified_gmt":"2007-09-25T02:15:00","slug":"how-can-i-change-the-paragraph-case-in-microsoft-word","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-paragraph-case-in-microsoft-word\/","title":{"rendered":"How Can I Change the Paragraph Case in Microsoft Word?"},"content":{"rendered":"<p><IMG 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\"> \n<P>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\u2019s 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?<BR><BR>&#8212; JH<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG 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 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> \n<P>Hey, JH. We don\u2019t know if you pay much attention to public education here in the state of Washington (heaven knows that the <I>students<\/I> don\u2019t seem to pay much attention to public education here in the state of Washington) but the state recently \u2013 and proudly \u2013 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\u2019s on us!<\/P>\n<P>Of course, that 83.6 percent doesn\u2019t include students who have already dropped out of school, and it doesn\u2019t include students who are still in school but have fallen behind on their credits and will <I>not<\/I> graduate on time. Oh, and it doesn\u2019t 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 <I>still<\/I> they couldn\u2019t figure out how to get a 100% graduation rate.)<\/P>\n<P>The Scripting Guys\u2019 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\u2019s 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?<\/P>\n<P>That\u2019s easy: we simply changed the law and said, \u201cNever mind; turns out that you can flunk the math portion of the WASL and graduate after all.\u201d Problem solved.<\/P>\n<P>Besides, who uses math in <I>this<\/I> day and age?<\/P>\n<P>Judging by the number of bankruptcies, loan defaults, and foreclosures that occur each and every day, no one.<\/P>\n<P>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\u2019s due, at least in part, to the fact that they didn\u2019t have the WASL back in those days. (Come to think of it, they didn\u2019t 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\u2019t. Fortunately for us, however, and in the spirit of the modern-day student, we <I>were<\/I> able to steal one off the Internet instead:<\/P><PRE class=\"codeSample\">Const wdTitleWord = 2\nConst wdTitleSentence = 4<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)<\/p>\n<p>Set colParagraphs = objDoc.Paragraphs<\/p>\n<p>For Each objParagraph in colParagraphs\n    If objParagraph.Range.Font.Size = 14 Then\n        objParagraph.Range.Case = wdTitleWord\n    Else\n        objParagraph.Range.Case = wdTitleSentence\n    End If\nNext\n<\/PRE>\n<P>As you can see, we (well, OK, whoever actually <I>wrote<\/I> 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.<\/P>\n<P>Um, as we were saying, we\u2019ll use wdTitleWord to apply the title case to all the headings in the document; that\u2019s going to capitalize the first letter of each word in those headings. Meanwhile, we\u2019ll 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 <B>.<\/B> or <B>!<\/B> or <B>?<\/B>) will be capitalized; all the other words in the sentence will remain lowercase.<\/P>\n<P>Admittedly, this is not a foolproof solution. (But don\u2019t blame us; we didn\u2019t write it!) For example, suppose you have the following sentence in your document:<\/P>\n<P>i live in new york city.<\/P>\n<P>After we apply sentence casing, that same sentence will look like this:<\/P>\n<P>I live in new york city.<\/P>\n<P>That\u2019s better, but it\u2019s not perfect; after all, that should be <B>N<\/B>ew <B>Y<\/B>ork <B>C<\/B>ity, not <B>n<\/B>ew <B>y<\/B>ork <B>c<\/B>ity. But this is about the best we can hope for.<\/P>\n<P>After we define our constants we create an instance of the <B>Word.Application<\/B> object and set the <B>Visible<\/B> 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 <B>Open<\/B> method) to open the document C:\\Scripts\\Test.doc:<\/P><PRE class=\"codeSample\">Set objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)\n<\/PRE>\n<P>In case you\u2019re wondering, that document looks like this:<\/P><IMG height=\"385\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/changecase1.jpg\" width=\"347\" border=\"0\"> \n<P><BR>See all those lowercase letters everywhere you look? That\u2019s what we need to fix.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">Set colParagraphs = objDoc.Paragraphs\n<\/PRE>\n<P>Once we\u2019ve done that we then use <I>this<\/I> line of code to set up a For Each loop to walk us through each paragraph in that collection:<\/P><PRE class=\"codeSample\">For Each objParagraph in colParagraphs\n<\/PRE>\n<P>Still with us? Good. Because now we\u2019re ready for the fun part.<\/P>\n<P>Inside the loop, we use the following line of code to determine whether the paragraph (or, more correctly, the paragraph\u2019s <B>Range<\/B>) uses a font size of 14 points:<\/P><PRE class=\"codeSample\">If objParagraph.Range.Font.Size = 14 Then\n<\/PRE>\n<P>Suppose the paragraph <I>does<\/I> 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\u2019s <B>Case<\/B> property to the constant wdTitleWord, something that applies the title case to the paragraph:<\/P><PRE class=\"codeSample\">objParagraph.Range.Case = wdTitleWord\n<\/PRE>\n<P>And what if the paragraph <I>doesn\u2019t<\/I> 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:<\/P><PRE class=\"codeSample\">objParagraph.Range.Case = wdTitleSentence\n<\/PRE>\n<P>And then we loop around and repeat the process with the next paragraph in the collection. By the time we\u2019re finished looping our document should look like this:<\/P><IMG height=\"385\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/changecase2.jpg\" width=\"347\" border=\"0\"> \n<P><BR>Which is just exactly what we want it to look like.<\/P>\n<P>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\u2019d 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:<\/P>\n<P>\u201cNo, $11 a day is more than fair. Do you have any idea how big a number 11 really is? That\u2019s what we thought.\u201d<\/P>\n<P>Just something to keep in mind.<\/P>\n<TABLE class=\"dataTable\" id=\"E3G\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Ed<\/B><B>itor\u2019s Note:<\/B> If today\u2019s column <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0425.mspx\"><B>sounded familiar<\/B><\/A> to you, you\u2019re obviously a devoted reader of the <I>Hey, Scripting Guy!<\/I> column \u2013 and much farther from senility than the Scripting Guy who writes this column.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&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-63953","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! 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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63953","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=63953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63953\/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=63953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}