Inserting a row with a form field.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a table setup withtwo columns to insert a new row when you tab through
the las column. Using this code:
With Selection
If .Information(wdWithInTable) Then
.Tables(1).Rows.Last.Select
.InsertRowsBelow 1
End If
End With

And all is good, except that I would like to create the new row with a 'Text
Form Field' in each column.

The last row of my table already has the form fields. So is it possible to,
when inserting the new row, to have it also insert the form fields or maybe
have it copy the filds from the previouse row?

thx
 
The following macro does what you want. Watch out for the linebreaks
inserted by the mail program.



Sub addrow()

' Macro created 02/02/03 by Doug Robbins

' To add a new row to a table containing formfields in every column

' automatically on exit from the last cell in the present last row of the
table

Dim rownum As Integer, i As Integer

ActiveDocument.Unprotect

ActiveDocument.Tables(1).Rows.Add

rownum = ActiveDocument.Tables(1).Rows.Count

For i = 1 To ActiveDocument.Tables(1).Columns.Count

ActiveDocument.FormFields.Add
Range:=ActiveDocument.Tables(1).Cell(rownum, i).Range,
Type:=wdFieldFormTextInput

Next i

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
ActiveDocument.Tables(1).Columns.Count).Range.FormFields(1).ExitMacro =
"addrow"

ActiveDocument.Tables(1).Cell(ActiveDocument.Tables(1).Rows.Count,
1).Range.FormFields(1).Select

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True



End Sub

--
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
 
Stikfa said:
I have a table setup withtwo columns to insert a new row when you tab
through the las column. Using this code:
With Selection
If .Information(wdWithInTable) Then
.Tables(1).Rows.Last.Select
.InsertRowsBelow 1
End If
End With

And all is good, except that I would like to create the new row with
a 'Text Form Field' in each column.

The last row of my table already has the form fields. So is it
possible to, when inserting the new row, to have it also insert the
form fields or maybe have it copy the filds from the previouse row?

thx

Hi Stikfa,

The same macro that adds the row can also insert new fields in it. The
following code is a bit more complicated than what you showed, but it has
two main advantages: (1) it doesn't move the Selection, which corresponds to
the insertion point in the document, and (2) it will insert the right number
of fields, regardless of how many columns your table has.

Dim myTable As Table
Dim myRow As Row
Dim ColNum As Long

If Not Selection.Information(wdWithInTable) Then
Exit Sub ' no table; don't do any more
End If

' make an onject that represents the table
Set myTable = Selection.Tables(1)

' add a row to the bottom of the table
' and make an object that represents the row
Set myRow = myTable.Rows.Add

' use a loop to insert a form field into
' each cell in the new row
For ColNum = 1 To myTable.Columns.Count
ActiveDocument.FormFields.Add _
Range:=myRow.Cells(ColNum).Range, _
Type:=wdFieldFormTextInput
Next ColNum

' clean up
Set myRow = Nothing
Set myTable = Nothing
 
Back
Top