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”Ībout the only thing to take note of here is this line of code: Set objTable = objDoc.Tables(2)Īs you can see, this time around we’re creating an object reference that points to a table with the index number 2. That simply positions the cursor on a new, blank line, enabling us to add a new table header and, following that, a new table. We then use the TypeParagraph() method to simulate pressing the ENTER key on the keyboard. Which spot? The very end of the document, as indicated by the constant END_OF_STORY. All we’re doing here is calling the EndKey method to move the cursor to a new spot in the document. Granted it doesn’t look very impressive, but it works. If it’s not, though, then here’s the secret to adding a second table to the document: objSelection.EndKey END_OF_STORY We agree: that should be enough for anyone. That all sounds complicated, but it requires only three little lines of code: Set objRange = objSelection.RangeĪnd 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.”Īll that effort gives us a Word document that looks like this: 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). 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). 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.
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”Īt this point we’re ready to insert our first table. Good guess: it’s now that the fun begins. 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 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.
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. (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 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. ObjTable.Cell(1, 2).Range.Text = “This is cell 2.” ObjTable.Cell(1, 1).Range.Text = “This is cell 1.” Set objWord = CreateObject(“Word.Application”) Use it at your own risk, KH: Const END_OF_STORY = 6 With those people in mind, we tossed together a sample script that adds two tables to a Word document. 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. One table per document is plenty, thank you very much. Create a Microsoft Word document with multiple tables? No, sir that’s not for us, not at all. You know, if the Scripting Guys have one failing (hey, we said if) it’s this: we’re simple guys with simple tastes. Hey, Scripting Guy! How can I add multiple tables to a Word document?