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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add row at bottom of table (MS WORD)

Status
Not open for further replies.

richiwatts

Technical User
Jun 21, 2002
180
GB
I created a very basic Macro to add a row at the bottom of a table in MS Word:

Sub Newissue()
Selection.InsertRowsBelow 1
End Sub


However, this will only work if the curser is in the last row of the table. So if I just open the document and run the Macro I get a run time error because the curser is not in the table anywhere. If I put the curser in one of the rows that is not the last row I end up with a row being inserted in the wrong place.

How can I be sure that on running a Macro that a row will be inserted at the end of the table without having to place the curser in the last row?

Many thanks
Richi
 
Hi richiwatts,

To do this you MUST have the last row of the table SELECTed. If the cursor is anywhere in the table then this will do it:

Code:
Selection.Tables(1).Select
Selection.InsertRowsBelow 1

If you would like to leave the cursor where it started out you can wrap it up like this:

Code:
Dim rngTemp As Range
Set rngTemp = Selection.Range
Code:
Selection.Tables(1).Select
Selection.InsertRowsBelow 1
Code:
rngTemp.Select

If the cursor is not in the table at all then you must have some way of identifying the table. If it is the only table in the document you can do this:

Code:
Dim rngTemp As Range
Set rngTemp = Selection.Range
Code:
ActiveDocument
Code:
.Tables(1).Select
Selection.InsertRowsBelow 1
rngTemp.Select

If your document has multiple tables you will need to bookmark it unless you know, for example, that it is always the first (or second or last, etc.) table in the document.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top