{"id":67143,"date":"2006-06-09T14:32:00","date_gmt":"2006-06-09T14:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/09\/how-can-i-add-multiple-tables-to-a-word-document\/"},"modified":"2006-06-09T14:32:00","modified_gmt":"2006-06-09T14:32:00","slug":"how-can-i-add-multiple-tables-to-a-word-document","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-multiple-tables-to-a-word-document\/","title":{"rendered":"How Can I Add Multiple Tables to a Word Document?"},"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 add multiple tables to a Word document?<BR><BR>&#8212; KH<\/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, KH. You know, if the Scripting Guys have one failing (hey, we said <I>if<\/I>) it\u2019s this: we\u2019re simple guys with simple tastes. Create a Microsoft Word document with multiple tables? No, sir; that\u2019s not for us, not at all. One table per document is plenty, thank you very much. <\/P>\n<P>Of course, we <I>do<\/I> realize that there are a lot of wild and crazy people out there, daredevils willing to try just about anything, up to and including adding multiple tables to a single Word document. With those people in mind, we tossed together a sample script that adds <I>two<\/I> tables to a Word document. Use it at your own risk, KH:<\/P><PRE class=\"codeSample\">Const END_OF_STORY = 6<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nobjWord.Visible = True<\/p>\n<p>Set objDoc = objWord.Documents.Add()\nSet objSelection = objWord.Selection<\/p>\n<p>objSelection.TypeText &#8220;Table 1&#8221;\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objSelection.Range\nobjDoc.Tables.Add objRange, 1, 2\nSet objTable = objDoc.Tables(1)<\/p>\n<p>objTable.Cell(1, 1).Range.Text = &#8220;This is cell 1.&#8221;\nobjTable.Cell(1, 2).Range.Text = &#8220;This is cell 2.&#8221;\nobjSelection.EndKey END_OF_STORY\nobjSelection.TypeParagraph()<\/p>\n<p>objSelection.TypeText &#8220;Table 2&#8221;\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objSelection.Range\nobjDoc.Tables.Add objRange, 1, 2\nSet objTable = objDoc.Tables(2)<\/p>\n<p>objTable.Cell(1, 1).Range.Text = &#8220;This is cell 1.&#8221;\nobjTable.Cell(1, 2).Range.Text = &#8220;This is cell 2.&#8221;<\/p>\n<p>objSelection.EndKey END_OF_STORY\nobjSelection.TypeParagraph()\n<\/PRE>\n<P>In the relatively small amount of time we spent on this (too much excitement angries up the blood, you know) we decided that the key to adding multiple tables to a single document was this: after adding the first table you need to move the cursor to the end of the document, type a paragraph return, and <I>then<\/I> add the second table. (We weren\u2019t brave enough to try adding a <I>third<\/I> table, but it should work the same way.) That\u2019s the approach we took; let\u2019s see how it actually works.<\/P>\n<P>In the script itself we start off by creating a constant named END_OF_STORY; as the name implies, we\u2019ll use this constant to move the cursor to the end of the document. After defining the constant 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 Word that we can see onscreen. We use the <B>Add<\/B> method to add a new, blank document, and then position the cursor at the beginning of that document simply by creating an instance of the Word <B>Selection<\/B> object:<\/P><PRE class=\"codeSample\">Set objSelection = objWord.Selection\n<\/PRE>\n<P>Good guess: it\u2019s <I>now<\/I> that the fun begins. Our first task is to create a table header; to do that we simply type the phrase <I>Table 1<\/I> followed by a paragraph return:<\/P><PRE class=\"codeSample\">objSelection.TypeText &#8220;Table 1&#8221;\nobjSelection.TypeParagraph()\n<\/PRE>\n<P>At this point we\u2019re ready to insert our first table. We aren\u2019t going to explain all of the following code in detail; if you\u2019ve never added a table to a Word document you might want to take a look at the <I>Office Space<\/I> article <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/officetips\/jul05\/tips0726.mspx\"><B>Creating Tables in Microsoft Word<\/B><\/A>. For now we\u2019ll simply note that we begin by creating an instance of the <B>Range<\/B> object (with the range consisting of the current cursor location). After that we add a table with 1 row and 2 columns, and then create an object reference (objTable) to that table (which, because it\u2019s the first table in the document, is given the index number 1). That all sounds complicated, but it requires only three little lines of code:<\/P><PRE class=\"codeSample\">Set objRange = objSelection.Range\nobjDoc.Tables.Add objRange, 1, 2\nSet objTable = objDoc.Tables(1)\n<\/PRE>\n<P>And once we have a table we can then use these lines of code to type a little bit of text in each of the two table cells:<\/P><PRE class=\"codeSample\">objTable.Cell(1, 1).Range.Text = &#8220;This is cell 1.&#8221;\nobjTable.Cell(1, 2).Range.Text = &#8220;This is cell 2.&#8221;\n<\/PRE>\n<P>All that effort gives us a Word document that looks like this:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/multitables1.jpg\" width=\"443\" height=\"367\"> \n<P><BR>We agree: that should be enough for <I>anyone<\/I>. If it\u2019s not, though, then here\u2019s the secret to adding a second table to the document:<\/P><PRE class=\"codeSample\">objSelection.EndKey END_OF_STORY\nobjSelection.TypeParagraph()\n<\/PRE>\n<P>Granted it doesn\u2019t look very impressive, but it works. All we\u2019re doing here is calling the <B>EndKey<\/B> method to move the cursor to a new spot in the document. Which spot? The very end of the document, as indicated by the constant END_OF_STORY. We then use the <B>TypeParagraph()<\/B> method to simulate pressing the ENTER key on the keyboard. That simply positions the cursor on a new, blank line, enabling us to add a new table header and, following that, a new table.<\/P>\n<P>In fact, that\u2019s what this block of code does: it adds a header for <I>Table 2<\/I>, and then inserts the second table in the document:<\/P><PRE class=\"codeSample\">objSelection.TypeText &#8220;Table 2&#8221;\nobjSelection.TypeParagraph()<\/p>\n<p>Set objRange = objSelection.Range\nobjDoc.Tables.Add objRange, 1, 2\nSet objTable = objDoc.Tables(2)<\/p>\n<p>objTable.Cell(1, 1).Range.Text = &#8220;This is cell 1.&#8221;\nobjTable.Cell(1, 2).Range.Text = &#8220;This is cell 2.&#8221;\n<\/PRE>\n<P>About the only thing to take note of here is this line of code:<\/P><PRE class=\"codeSample\">Set objTable = objDoc.Tables(2)\n<\/PRE>\n<P>As you can see, this time around we\u2019re creating an object reference that points to a table with the index number 2. Why? Because this is the second table in the document. If we add a third table, we have to create an object reference to a table with an index number of 3: <B>Set objTable = objDoc.Tables(3)<\/B>. And if we add a fourth table &#8211; well, let\u2019s not even go <I>there<\/I>, if you know what we mean. But you get the idea.<\/P>\n<P>After we add the new table and type a little text into the table cells we once again position the cursor at the end of the document:<\/P><PRE class=\"codeSample\">objSelection.EndKey END_OF_STORY\nobjSelection.TypeParagraph()\n<\/PRE>\n<P>And what does all that give us? This:<\/P><IMG border=\"0\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/multitables2.jpg\" width=\"443\" height=\"367\"> \n<P><BR>Which we believe is exactly what you were hoping to get.<\/P>\n<TABLE id=\"ECG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Disclaimer<\/B>. In the interests of full disclosure we should note that we did not actually test this multiple-table script ourselves; instead, we had our stunt doubles test it for us. Not that we were <I>scared<\/I> or anything, but \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add multiple tables to a Word document?&#8212; KH Hey, KH. You know, if the Scripting Guys have one failing (hey, we said if) it\u2019s this: we\u2019re simple guys with simple tastes. Create a Microsoft Word document with multiple tables? No, sir; that\u2019s not for us, not at all. One [&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,395],"class_list":["post-67143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-word","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add multiple tables to a Word document?&#8212; KH Hey, KH. You know, if the Scripting Guys have one failing (hey, we said if) it\u2019s this: we\u2019re simple guys with simple tastes. Create a Microsoft Word document with multiple tables? No, sir; that\u2019s not for us, not at all. One [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67143","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=67143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67143\/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=67143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}