How do I copy/paste correct nbr of formfields using VBA and tables

J

Jocelyn

I have a Word document comprised of a table with 4 rows. Upon the click of a
button on the document, I need to copy one of the rows and insert it as a new
row in the table. This new row must be blank and ready to handle new input
data. However, no matter how I approach this, if I do the copy/paste after
data has been entered in the original row, the new cell will not recognize
the full number of formfields. Specifically: the table.row.cell I need to
copy/paste has 100 formfields. If data is entered into 25 of them, when the
table.row.cell is copied/pasted, the formfields.count for the new
table.row.cell is 75, and the previously entered data is copied over as well.
I need the new table.row.cell to have 100 formfields, so I can affix new
labels for them and so they can accept new data.
How can I accomplish this?
 
D

Doug Robbins

Hi Jocelyn,

Maybe this macro will do what you want

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
 

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