Loop to insert rows in a word table

J

Jesse Yoburn

Hi all-

I have a table set up in MS Word with text fields populating every
cell. I need to write a macro that, among other tasks, inserts a new
BLANK row after each row in the table.

That is, I have this:
--
aaa bbb ccc ddd eee
fff ggg hhh iii jjj
--

But need this:
--
aaa bbb ccc ddd eee

fff ggg hhh iii jjj

--

The table is quite large and will be growing regularly, so I'll need to
run the macro every few days and re-create this new formatting. I've
done some work with macros in the past but loops have always been
trouble for me.

Thanks in advance for your help!
 
H

Helmut Weber

Hi Jesse,

it seems, one needs the selection for this:

Sub Test0987()
Dim r As Long ' row
Dim t As Table
Set t = Selection.Tables(1)
For r = 1 To t.Rows.Count * 2 Step 2
t.Cell(r, 1).Select
Selection.InsertRowsBelow 1
Next
End Sub

To be used wisely!

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

Doug Robbins - Word MVP

Always better/quicker to avoid using the Selection

Dim i As Long
With ActiveDocument.Tables(1)
.Rows.Add
For i = .Rows.Count - 1 To 2 Step -1
.Rows.Add BeforeRow:=.Rows(i)
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top