Copy row containing different fields

D

Dylan

Hi,

I've been able to set up an exit macro on previous projects to create a new
row in a protected form. In this case I have different fields that I want to
copy.

The table contains three columns each with the following fields (starting at
row 2).
The first row Cell has a numbered field "1" and incrimenting by +1 as each
new row is added.
The second cell is a textfield, straightforward to insert.
The third has a dropdown list containing "1" and "2" I hope to be able to do
this on my own.

My main problem is making the first cell incremental as each new row is
added, I don't know what type of field to use, or how to programme it so that
the number increases at each new row.
 
D

Doug Robbins - Word MVP

Rather than using fields, it might be just as simple to use code to get the
number from the present first cell in the last row of the table and then add
1 to it and then use that number as the .Text for the .Range of the first
cell in the new row after you add it.

If however you are actually copying the row then if you have a SEQ field in
the first cell that will increment by one each time you paste at new row.

--
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
 
D

Dylan

Hi Doug,

I had a go at it, but can't get it to work.

Before adding a new row, I'm trying to use
myCelltxt = Selection.Tables(1).Rows.Last.Cells(1).Range.Text
To determine the number value in the first cell of the last row

After adding a new row, I'm trying to use
Selection.Cells(1).Range.Text = myCelltxt + 1
To input text which increases the value by one.

The new row is added, not copied, by the way.

Regards
Dylan
 
D

Doug Robbins - Word MVP

Use

Dim myCelltxt as Range
Dim newrow as Row
With Selection.Tables(1)
Set myCelltxt = .Rows.Last.Cells(1).Range
myCelltxt.End = myCelltxt.End - 1
Set newrow = .Rows.Add
newrow.Cells(1).Range.Text = Val(myCelltxt.Text) + 1
End With

--
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
 
D

Dylan

Hi Doug

Can you tell me how to add Formfields to cells(2) and (3)

I've tried adding the requirements after newrow.Cells(1).Range...

Set newrow = .Rows.Add
newrow.Cells(1).Range.Text = Val(myCelltxt.Text) + 1
newrow.Cells(2).Range.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldFormTextInput 'etc...

And after adding the new row setting newrow to the last row...
Set newrow = .Rows.Add
Set newrow = ActiveDocument.Tables(4).Rows.Last
newrow.Cells(1).Range.Text = Val(myCelltxt.Text) + 1
newrow.Cells(2).Range.FormFields.Add Range:=Selection.Range, _
Type:=wdFieldFormTextInput 'etc...

Much appreciated
Dylan
 
D

Doug Robbins - Word MVP

' 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
 
D

Dylan

Thanks Doug

I managed to get it using your code including a couple of If statements to
format each cell's formfield.

Dylan
 

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