How Can I Add Multiple Tables to a Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add multiple tables to a Word document?

— KH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KH. You know, if the Scripting Guys have one failing (hey, we said if) it’s this: we’re simple guys with simple tastes. Create a Microsoft Word document with multiple tables? No, sir; that’s not for us, not at all. One table per document is plenty, thank you very much.

Of course, we do 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 two tables to a Word document. Use it at your own risk, KH:

Const END_OF_STORY = 6

Set objWord = CreateObject(“Word.Application”) objWord.Visible = True

Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection

objSelection.TypeText “Table 1” objSelection.TypeParagraph()

Set objRange = objSelection.Range objDoc.Tables.Add objRange, 1, 2 Set objTable = objDoc.Tables(1)

objTable.Cell(1, 1).Range.Text = “This is cell 1.” objTable.Cell(1, 2).Range.Text = “This is cell 2.” objSelection.EndKey END_OF_STORY objSelection.TypeParagraph()

objSelection.TypeText “Table 2” objSelection.TypeParagraph()

Set objRange = objSelection.Range objDoc.Tables.Add objRange, 1, 2 Set objTable = objDoc.Tables(2)

objTable.Cell(1, 1).Range.Text = “This is cell 1.” objTable.Cell(1, 2).Range.Text = “This is cell 2.”

objSelection.EndKey END_OF_STORY objSelection.TypeParagraph()

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 then add the second table. (We weren’t brave enough to try adding a third table, but it should work the same way.) That’s the approach we took; let’s see how it actually works.

In the script itself we start off by creating a constant named END_OF_STORY; as the name implies, we’ll use this constant to move the cursor to the end of the document. After defining the constant we create an instance of the Word.Application object and set the Visible property to True; that gives us a running instance of Word that we can see onscreen. We use the Add 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 Selection object:

Set objSelection = objWord.Selection

Good guess: it’s now that the fun begins. Our first task is to create a table header; to do that we simply type the phrase Table 1 followed by a paragraph return:

objSelection.TypeText “Table 1”
objSelection.TypeParagraph()

At this point we’re ready to insert our first table. We aren’t going to explain all of the following code in detail; if you’ve never added a table to a Word document you might want to take a look at the Office Space article Creating Tables in Microsoft Word. For now we’ll simply note that we begin by creating an instance of the Range 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’s 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:

Set objRange = objSelection.Range
objDoc.Tables.Add objRange, 1, 2
Set objTable = objDoc.Tables(1)

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:

objTable.Cell(1, 1).Range.Text = “This is cell 1.”
objTable.Cell(1, 2).Range.Text = “This is cell 2.”

All that effort gives us a Word document that looks like this:

Hey, Scripting Guy!


We agree: that should be enough for anyone. If it’s not, though, then here’s the secret to adding a second table to the document:

objSelection.EndKey END_OF_STORY
objSelection.TypeParagraph()

Granted it doesn’t look very impressive, but it works. All we’re doing here is calling the EndKey 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 TypeParagraph() 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.

In fact, that’s what this block of code does: it adds a header for Table 2, and then inserts the second table in the document:

objSelection.TypeText “Table 2”
objSelection.TypeParagraph()

Set objRange = objSelection.Range objDoc.Tables.Add objRange, 1, 2 Set objTable = objDoc.Tables(2)

objTable.Cell(1, 1).Range.Text = “This is cell 1.” objTable.Cell(1, 2).Range.Text = “This is cell 2.”

About the only thing to take note of here is this line of code:

Set objTable = objDoc.Tables(2)

As you can see, this time around we’re 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: Set objTable = objDoc.Tables(3). And if we add a fourth table – well, let’s not even go there, if you know what we mean. But you get the idea.

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:

objSelection.EndKey END_OF_STORY
objSelection.TypeParagraph()

And what does all that give us? This:

Hey, Scripting Guy!


Which we believe is exactly what you were hoping to get.

Disclaimer. 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 scared or anything, but ….

0 comments

Discussion is closed.

Feedback usabilla icon