brilliant
brilliant

Reputation: 2853

VBS: Appending two lines to MSWord table. How to do it?

Here I learned about a VBS script that would append a line to my MS Word table. The line to be added contains four cells, each cell containing one word:

turtle,cheetah,rooster,maple

The script works fine. It goes like this:

Set wd = CreateObject("Word.Application")    
wd.Visible = True    
Set doc = wd.Documents.Open ("E:\my_folder\add_to_this_table.doc")    
Set r = doc.Tables(1).Rows.Add    
aa = Split("turtle,cheetah,rooster,maple", ",")    
For i = 0 To r.Cells.Count - 1    
  r.Cells(i + 1).Range.Text = aa(i)    
Next

Now, what if I need to add two lines to this table?

The second line would be:

iguana,bear,hawk,alder

I tried it this way, but it doesn't work:

Set wd = CreateObject("Word.Application")    
wd.Visible = True    
Set doc = wd.Documents.Open ("E:\my_folder\add_to_this_table.doc")    
Set r = doc.Tables(1).Rows.Add    
aa = Split("turtle,cheetah,rooster,maple", ",")    
For i = 0 To r.Cells.Count - 1    
  r.Cells(i + 1).Range.Text = aa(i)    
Set r = doc.Tables(1).Rows.Add    
bb = Split("iguana,bear,hawk,alder", ",")    
For i = 0 To r.Cells.Count - 1    
  r.Cells(i + 1).Range.Text = bb(i)    
Next

What am I doing wrong here?

Upvotes: 0

Views: 1172

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

You forgot the Next for your first For loop.

Upvotes: 1

Related Questions