Tolure
Tolure

Reputation: 879

Excel VBA adding lines to a word document table

I'm trying to find a way to add lines and information to a prebuilt table in a word document. The table is right now only two lines. The headers and a blank line (2 Columns). How would I go about adding new lines of data to this table. I've tried with bookmarks but I have been unable to get the code to work.

I would give a sample of code but my code just fails so I don't think it would help.

I need to be able to loop through a 2-D array and insert new rows with information from the 2-D array for each time it loops.

Upvotes: 0

Views: 4167

Answers (1)

Stewbob
Stewbob

Reputation: 16899

This is the basic syntax for inserting data into a pre-existing table in Word, from VBA in Excel:

Dim doc As Word.Document
Set doc = GetObject("path to my word document")

With doc.Tables(IndexNumberOfTableYouWant)
  .Cell(DesiredRow, 1).Range.Text = "my column 1 information"
  .Cell(DesiredRow, 2).Range.Text = "my column 2 information"
End With

As long as the proper number of columns is predefined, you can just keep adding rows by incrementing the DesiredRow variable, and Word will append those rows to the table automatically.

Upvotes: 2

Related Questions