I have a word document in which I'm trying to select the first cell of the last row of a table. I keep getting an error (red circle with white 'X') that has no text.
Here's my code:
The error is occurring at the "Selection.Tables(4).Rows(lastrow).Select" line but as you can see from the commented areas, I've tried it in other places too with the same results.
Any ideas what I'm doing wrong?
Here's my code:
Code:
Sub IncrementTable()
ActiveDocument.Unprotect
Selection.Find.ClearFormatting
With Selection.Find
.Text = "REV."
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Rows.Delete
ActiveDocument.Tables(4).Rows.Add
MsgBox ("Last row in table 4 = " & ActiveDocument.Tables(4).Rows.Count & vbNewLine & _
"Last row (alternate) = " & Selection.Information(wdMaximumNumberOfRows))
Dim lastrow As Long
lastrow = ActiveDocument.Tables(4).Rows.Count
'Selection.Tables(4).Cell(lastrow, 1).Select
'Selection.MoveDown Unit:=wdLine, Count:=12
For i = 1 To 7
Selection.Tables(4).Rows(lastrow).Select
Selection.Collapse
Set ffield = Selection.FormFields.Add( _
Range:=Selection.Range, _
Type:=wdFieldFormTextInput)
ffield.Name = ""
Selection.MoveRight Unit:=wdCell, Count:=1
Next i
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
The error is occurring at the "Selection.Tables(4).Rows(lastrow).Select" line but as you can see from the commented areas, I've tried it in other places too with the same results.
Any ideas what I'm doing wrong?