Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate table in Word Template with VBA?

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?

like image 338
Chris Day Avatar asked Feb 21 '23 09:02

Chris Day


1 Answers

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)

like image 152
Siddharth Rout Avatar answered Feb 28 '23 02:02

Siddharth Rout