Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mod VBA code for row insert on a MSWORD table

Status
Not open for further replies.

Thr33of4

Technical User
Sep 4, 2005
3
US
I was fortunate enough for finding this code relating to adding a row to the end of a table in MS Word, however I need it to add more than one row. Specifically I need it to add rows 3, 4 and 5 and all columns in those rows. Row 3 has 4 columns, row 4 has 2 columns and row 5 has 5 columns if that info is necessary.

Sub AddRow()

Dim oTable As Table
Dim oCell As Cell
Dim oPrevRow As Row, oNewRow As Row
Dim iColumn As Long

' Insert new row
Set oTable = ActiveDocument.Tables(2)
Set oPrevRow = oTable.Rows(oTable.Rows.Count)
oTable.Rows.Add
Set oNewRow = oTable.Rows(oTable.Rows.Count)

' Copy text to new row
For Each oCell In oPrevRow.Cells
iColumn = iColumn + 1
oNewRow.Cells(iColumn).Range = oPrevRow.Cells(iColumn).Range
Next

End Sub
 
Row 3 has 4 columns, row 4 has 2 columns and row 5 has 5 columns if that info is necessary.
Oh it is necessary alright. And tricky.

VBA can NOT work on rows with merged cells (which is required if there is a difference in the number of columns).

So. You will have to add each row independently, then adjust the row to match your requirements. You will need the precise cell parameters first. It will not be easy.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top