Populate table in Word Template with VBA?

32,371

To populate table, use this code

ActiveDocument.Tables(1).Cell(1, 1).Range.Text = "Blah Blah"

This will write to the first cell in the first table. Use a loop to fill the rest of the cells.

I would also recommend see this link.

Topic: Automating Word Tables for Data Insertion and Extraction

Link: http://msdn.microsoft.com/en-us/library/aa537149%28v=office.11%29.aspx#Y1254

Extract:

Summary: Learn how to automate the creation and formatting of tables in Word. Get information about optimizing performance, populating a table with data, formatting table structure, linking table data, and extracting data from a table. (25 printed pages)

Share:
32,371
Chris Day
Author by

Chris Day

Updated on July 06, 2020

Comments

  • Chris Day
    Chris Day almost 4 years

    I'm filling in a Word template with data that's been collected from user input. In particular a (variable) number of documents is chosen, and information about each document fills a row of a table.

    I've bookmarked several items in the template and successfully filled information in the header from my macro, but the table I'm not so sure with. I bookmarked the first cell and tried tabbing (with Chr(9)) through, and also tried passing an array. (In the template the table has only a first row. Usually tabbing past the last column creates an additional row.)

    I can retieve cell contents with

       Word.Application.ActiveDocument.Tables(1).Cell(3, 1).Range.Text
    

    but can't write to the any cell except the first, where I placed a bookmark.

    Can anyone offer a possible solution to populate the table?