{"id":69733,"date":"2005-05-25T08:22:00","date_gmt":"2005-05-25T08:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/25\/how-can-i-extract-word-paragraphs-that-use-a-specific-style\/"},"modified":"2005-05-25T08:22:00","modified_gmt":"2005-05-25T08:22:00","slug":"how-can-i-extract-word-paragraphs-that-use-a-specific-style","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-extract-word-paragraphs-that-use-a-specific-style\/","title":{"rendered":"How Can I Extract Word Paragraphs That Use a Specific Style?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I save (to a separate file) all the paragraphs in a Word document that are formatted using a particular style, like Heading 1?<BR><BR>&#8212; RN<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RN. You know, when we first read this question we thought, \u201cInteresting. But why would you ever want to do something like <I>that<\/I>?\u201d And then one of the Scripting Guys remembered the many\u2026happy\u2026hours he spent one week going through Word documents and manually copying and pasting all the headings, to create a master list of topics. Once we had remembered that, we thought, \u201cYou know, it would have been really nice to have a had a script like this back then. But because we had to suffer through that chore without a script, why shouldn\u2019t <I>everyone<\/I> have to suffer through that chore without a script?\u201d <\/P>\n<P>But then someone else made a good point: while the Scripting Guys probably <I>deserve<\/I> to suffer, most script writers don\u2019t. Therefore, we decided to come up with a script that can pull out selected paragraphs from a Word document. Here\u2019s a simple little script that checks each paragraph to see if it uses the <B>Heading 1<\/B> style. If the paragraph uses that style, the script leaves it alone; if the paragraph <I>doesn\u2019t<\/I> use that style, the script deletes it.<\/P>\n<P>Why? Well, this seemed like the easiest way to handle the issue: instead of copying Heading 1 paragraphs from one document to another, we just delete everything that isn\u2019t a Heading 1 from the document. When we\u2019re all done, we then use the <B>SaveAs<\/B> method to save the file under a separate file name. The net result? We still have the original document, plus we have a new document that contains only the paragraphs formatted as Heading 1.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\nSet objDoc = objWord.Documents.Open(&#8220;C:\\Scripts\\Test.doc&#8221;)<\/p>\n<p>Set objSelection = objWord.Selection\nSet colParagraphs = objDoc.Paragraphs<\/p>\n<p>For Each objParagraph in colParagraphs\n    If objParagraph.Style &lt;&gt; &#8220;Heading 1&#8221; Then\n        objParagraph.Range.Select\n        objSelection.Cut\n    End If    \nNext<\/p>\n<p>objDoc.SaveAs(&#8220;C:\\Scripts\\Headings.doc&#8221;)\nobjWord.Quit\n<\/PRE>\n<P>We begin by creating an instance of the <B>Word.Application<\/B> object and then set the <B>Visible<\/B> property to True; we do that just so everything that happens will be visible on screen. We then use the <B>Open<\/B> method to open the file C:\\Scripts\\Test.doc.<\/P>\n<TABLE id=\"E3D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Needless to say, if you decide to try this script your results will be far more interesting if Test.doc actually includes a few paragraphs that use the Heading 1 style.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We create an instance of the Word <B>Selection<\/B> object and then use this line of code to create an object reference to our document\u2019s <B>Paragraphs<\/B> collection:<\/P><PRE class=\"codeSample\">Set colParagraphs = objDoc.Paragraphs\n<\/PRE>\n<P>As you probably guessed, the Paragraphs collection contains all the paragraphs found in our document. Next we set up a For Each loop to loop through this collection. For each paragraph in the collection (and thus for each paragraph in the document) we use this line of code to see if the paragraph uses the Heading 1 style:<\/P><PRE class=\"codeSample\">If objParagraph.Style &lt;&gt; &#8220;Heading 1&#8221; Then\n<\/PRE>\n<P>If the paragraph <I>does<\/I> use the Heading 1 style, we simply loop back around and check the next item in the collection. But what if the paragraph <I>doesn\u2019t<\/I> have the Heading 1 style (that if, what if our If statement is true)? In that case we use these two lines of code to select the paragraph and then cut it from the document:<\/P><PRE class=\"codeSample\">objParagraph.Range.Select\nobjSelection.Cut\n<\/PRE>\n<P>After we\u2019ve looped through the entire collection &#8211; and deleted all the paragraphs that don\u2019t use the Heading 1 style &#8211; we\u2019ll be left with nothing but Heading 1 paragraphs. At that point all we have to do is call the SaveAs method and save the document under a new name (in this case, C:\\Scripts\\Headings.doc) and then exit Word. And that\u2019s what we do with these two lines of code:<\/P><PRE class=\"codeSample\">objDoc.SaveAs(&#8220;C:\\Scripts\\Headings.doc&#8221;)\nobjWord.Quit\n<\/PRE>\n<P>And, yes, we <I>do<\/I> feel better for having shared this with the world. (Although we still would have liked to make you suffer a little. But just a little, mind you.)<\/P>\n<P>Incidentally, if this whets your appetite for Microsoft Office scripting, be sure and check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/default.mspx\"><B>Office Space<\/B><\/A> column, published every Tuesday and Thursday.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I save (to a separate file) all the paragraphs in a Word document that are formatted using a particular style, like Heading 1?&#8212; RN Hey, RN. You know, when we first read this question we thought, \u201cInteresting. But why would you ever want to do something like that?\u201d And then [&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-69733","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! How can I save (to a separate file) all the paragraphs in a Word document that are formatted using a particular style, like Heading 1?&#8212; RN Hey, RN. You know, when we first read this question we thought, \u201cInteresting. But why would you ever want to do something like that?\u201d And then [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69733","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=69733"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69733\/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=69733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}