{"id":64803,"date":"2007-05-23T23:11:00","date_gmt":"2007-05-23T23:11:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/05\/23\/how-can-i-save-each-paragraph-in-a-word-document-as-a-database-record\/"},"modified":"2007-05-23T23:11:00","modified_gmt":"2007-05-23T23:11:00","slug":"how-can-i-save-each-paragraph-in-a-word-document-as-a-database-record","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-each-paragraph-in-a-word-document-as-a-database-record\/","title":{"rendered":"How Can I Save Each Paragraph in a Word Document as a Database Record?"},"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! A few days ago you showed us how to open a bunch of text files and save the data in each file as a record in a database. I have a similar question, except that I want to save each paragraph in a Word document as a database record. How can I do that?<BR><BR>&#8212; HY <\/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, HY. Before we tackle today\u2019s question we know a lot of you are concerned about Microsoft\u2019s recent purchase of online advertising giant <A href=\"http:\/\/aquantive.com\/\" target=\"_blank\"><B>aQuantive, Inc.<\/B><\/A>, and whether that will affect the Script Center in any way. Rest assured that there is nothing for you to worry about. In fact, as the good folks at Contoso Cola always say, \u201cDon\u2019t worry; drink Contoso.\u201d<\/P>\n<P>Contoso Cola. One sip and you\u2019ll think, \u201cThis is a <I>cola<\/I>?\u201d<\/P>\n<P>In other words, no, the fact that Microsoft spent $6 billion on an online advertising company will have no effect on the Script Center whatsoever.<\/P>\n<P>This 30-second Script Center update was brought to you by Tailspin Toys. Tailspin. Because anything else is just a toy.<\/P>\n<P>In case you\u2019re wondering, according to the press release this acquisition will definitely enhance the \u201cbroader advertising ecosystem.\u201d Now, to tell you the truth, the Scripting Guys have no idea what is meant by the \u201cbroader advertising ecosystem.\u201d We\u2019re hoping, however, that this means \u201cwriting a script that can save each paragraph in a Word document as a database record.\u201d If that\u2019s the case, then the Scripting Guys can help to enhance the broader advertising ecosystem, too:<\/P><PRE class=\"codeSample\">Const adLockOptimistic = 3<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>objConnection.Open _\n    &#8220;Provider = Microsoft.Jet.OLEDB.4.0; &#8221; &amp; _\n        &#8220;Data Source = C:\\Scripts\\Test.mdb&#8221; <\/p>\n<p>objRecordSet.Open &#8220;SELECT * FROM WordParagraphs&#8221; , _\n    objConnection, adOpenStatic, adLockOptimistic<\/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 objSelection = objWord.Selection<\/p>\n<p>For i = 1 to objDoc.Paragraphs.Count\n    objDoc.Paragraphs(i).Range.Select\n    If Len(objSelection.Text) &gt; 1 Then\n        objRecordSet.AddNew\n        objRecordSet(&#8220;ParagraphNumber&#8221;) = i\n        objRecordSet(&#8220;ParagraphText&#8221;) = objSelection.Text\n        objRecordSet.Update\n    End If\nNext\n<\/PRE>\n<P>Thanks to the peace of mind that comes when you\u2019re insured by Humongous Insurance, we should be able to take a few minutes to see if we can figure out how this script works. To begin with, we have a very simple little Word document (C:\\Scripts\\Test.doc), a document that looks like this:<\/P><IMG height=\"320\" alt=\"Microsoft Word\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/paragraphs.jpg\" width=\"375\" border=\"0\"> \n<P><BR>We also have a very simple little Access database (C:\\Scripts\\Test.mdb). This database includes a table named WordParagraphs; that table contains two fields:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ParagraphNumber<\/B>, a field we\u2019ll use to keep track of paragraphs by number (i.e, the first paragraph in the document is number 1, the second paragraph is number 2, etc.).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>ParagraphText<\/B>, a memo field where we\u2019ll stash the text from each paragraph.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>OK, let\u2019s roll up our sleeves and get down to business, the same outstanding business that Woodgrove Bank has provided for the past 50 years. Woodgrove Bank: We treat your money as if it was our money.<\/P>\n<P>We start out by defining a constant named adLockOptimistic and setting the value to 3; we\u2019ll use this constant to tell the database to lock a record only when that record is being updated. We then use the following block of code to create instances of the <B>ADODB.Connection <\/B>and <B>ADODB.Recordset<\/B> objects, and to open the datatase C:\\Scripts\\Test.mdb:<\/P><PRE class=\"codeSample\">Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>objConnection.Open _\n    &#8220;Provider = Microsoft.Jet.OLEDB.4.0; &#8221; &amp; _\n        &#8220;Data Source = C:\\Scripts\\Test.mdb&#8221;<\/PRE>\n<P>And then we use this line of code to grab a recordset consisting of all the records in the table WordParagraphs:<\/P><PRE class=\"codeSample\">objRecordSet.Open &#8220;SELECT * FROM WordParagraphs&#8221; , _\n    objConnection, adOpenStatic, adLockOptimistic<\/PRE>\n<TABLE class=\"dataTable\" id=\"EKE\" 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>. Yes, we did kind of zip through all that database stuff, didn\u2019t we? If you could use a little more background information on working with databases you might want to take a peek at the <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=22089\" target=\"_blank\"><B>Scripting Guys webcast<\/B><\/A> on that very subject.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>It\u2019s now time to shift gears a little bit and turn our attention to Microsoft Word. (Having trouble shifting gears? Then maybe it\u2019s time to call Margie\u2019s Travel at 555-1283.) To begin with, we use these two lines of code to create an instance of the <B>Word.Application<\/B> object and to make that instance visible on screen:<\/P><PRE class=\"codeSample\">Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True\n<\/PRE>\n<P>Once Word is up and running we use 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>After that we create an instance of the <B>Word.Selection<\/B> object, an object we\u2019ll need in order to grab the text from each paragraph. At this point we\u2019re ready to have some <I>real<\/I> fun, the same kind of fun offered each and every day at the Alpine Ski House.<\/P>\n<P>As it turns out, every Word document contains a <B>Paragraphs<\/B> collection; as the name implies, this is a collection of all the paragraphs found in the document. If we want to get information about each paragraph (which we do) then we need to set up a For Next loop that runs from 1 to the total number of paragraphs in the document (something we can determine by using the Paragraphs collection\u2019s <B>Count<\/B> property). Or, to put it a bit more programmatically:<\/P><PRE class=\"codeSample\">For i = 1 to objDoc.Paragraphs.Count\n<\/PRE>\n<P>What do we do inside the loop? Well to begin with, we\u2019re going to select the very first paragraph; that\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">objDoc.Paragraphs(i).Range.Select\n<\/PRE>\n<P>By calling the <B>Select<\/B> method against the paragraph range, we end up selecting the entire paragraph and automatically storing it in our object reference objSelection. That means that we can then check to see how many characters are in the <B>Text<\/B> property of objSelection:<\/P><PRE class=\"codeSample\">If Len(objSelection.Text) &gt; 1 Then\n<\/PRE>\n<P>Why do we do that? Well, if you look back at our Word document you\u2019ll see that we have blank lines between each paragraph. However, these lines aren\u2019t truly blank, at least not in the sense that there\u2019s absolutely nothing there. Instead, each \u201cblank\u201d line contains one of Word\u2019s paragraph marks. What we\u2019re doing with this line of code is checking to see if the paragraph text has more than one character (the <B>Len<\/B> function, of course, tells us the number of characters in a string). If the Text length is equal to 1 we\u2019re going to assume that this is a blank line and simply skip it; we\u2019ll only save the paragraph to the database if the Text has more than 1 character.<\/P>\n<TABLE class=\"dataTable\" id=\"EIG\" 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>. This is probably a good time to note that this script works only with straight text documents. If you have a bunch of tables in your document we can\u2019t guarantee that this script will work. It might work, but, then again, it might not.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s assume that we have a \u201creal\u201d paragraph here, a paragraph containing more than a single character. In that case we execute this block of code:<\/P><PRE class=\"codeSample\">objRecordSet.AddNew\nobjRecordSet(&#8220;ParagraphNumber&#8221;) = i\nobjRecordSet(&#8220;ParagraphText&#8221;) = objSelection.Text\nobjRecordSet.Update\n<\/PRE>\n<P>As you probably figured out, this is the code that saves the paragraph text as a record in the database. To do that we start out by creating a new, blank record; that\u2019s why we call the <B>AddNew<\/B> method. We then assign the value of our counter variable i to the ParagraphNumber field and the paragraph text (objSelection.Text) to the ParagraphText field. With the values assigned we then call the <B>Update<\/B> method to actually write the new record to the database.<\/P>\n<P>From there we loop around and repeat the process with the next paragraph in the collection.<\/P>\n<P>That should do the trick, HY. If it doesn\u2019t, feel free to email use courtesy of the Fabrikam Furniture Script Center. Fabrikam: The most trusted name in home furniture is now the most trusted name in system administration scripting. <BR><\/P>\n<TABLE class=\"dataTable\" id=\"EBH\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. OK, don\u2019t send any angry emails to Microsoft. We made this all up; we have not sold out and become the Fabrikam Furniture Script Center.<\/P>\n<P>Although we <I>are<\/I> willing to listen to offers \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! A few days ago you showed us how to open a bunch of text files and save the data in each file as a record in a database. I have a similar question, except that I want to save each paragraph in a Word document as a database record. How can I [&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-64803","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! A few days ago you showed us how to open a bunch of text files and save the data in each file as a record in a database. I have a similar question, except that I want to save each paragraph in a Word document as a database record. How can I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64803","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=64803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64803\/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=64803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}