#1
|
|||
|
|||
Automatically adding new rows based on form field
Hi,
I am trying to create a form field document which displays a price of a product which is to be paid in a certain number of installments. I want to automate the number of rows to be added automatically along with the form field options in it. For eg: if there are 24 installments, 24 new rows are to be added below with the given form field options. Need help from the experts as i tried some codes but they are not working. And i am not that good with word VBA. Attached is the sample file containing the field where the number of installments are to be added and the form fields that are to be added to the new installment rows. Thank you in advance. |
#2
|
|||
|
|||
Why are you mixing formfields and CCs. You can do this with CCs and a restricted form:
Code:
Select Case ContentControl.Title Case "Installments" If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Set oTbl = Selection.Tables(1) For lngCount = oTbl.Rows.Count To 4 Step -1 oTbl.Rows.Last.Delete Next lngCount For lngCount = 2 To CLng(ContentControl.Range.Text) oTbl.Rows.Last.Range.Copy oTbl.Rows.Last.Range.Paste Next lngCount For lngCount = 1 To CLng(ContentControl.Range.Text) If IsNumeric(oTbl.Rows(1).Cells(2).Range.ContentControls(1).Range.Text) Then oTbl.Rows(lngCount + 2).Cells(2).Range.ContentControls(1).Range.Text = _ oTbl.Rows(1).Cells(2).Range.ContentControls(1).Range.Text / CLng(ContentControl.Range.Text) Else oTbl.Rows(lngCount + 2).Cells(2).Range.ContentControls(1).Range.Text = "$0.00" End If Next lngCount oTbl.Range.Fields.Update oTbl.Rows(3).Cells(2).Range.ContentControls(1).Range.Select ActiveDocument.Protect wdAllowOnlyReading, True End Select End Sub |
#3
|
|||
|
|||
Thank you
Thank you Greg,
My 90% problem is solved. However, when i tried to impliment the code in my form sheet, i am facing an issue. The installment number in the table remains 1 for all the installment. The table that you created changed the number for all the installment, however, the same is not working in the document. I made some changes in the code while using it in my form, did i make any mistake in entering the code? Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Dim lngCount As Long Dim oTbl As Word.Table Select Case ContentControl.Title Case "Installments" If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect Set oTbl = Bookmarks("Inst1").Range.Tables(1) For lngCount = oTbl.Rows.Count To 5 Step -1 oTbl.Rows.Last.Delete Next lngCount For lngCount = 2 To CLng(ContentControl.Range.Text) oTbl.Rows.Last.Range.Copy oTbl.Rows.Last.Range.Paste Next lngCount For lngCount = 1 To CLng(ContentControl.Range.Text) If IsNumeric(oTbl.Rows(2).Cells(2).Range.ContentControls(1).Range.Text) Then oTbl.Rows(lngCount + 3).Cells(2).Range.ContentControls(1).Range.Text = _ oTbl.Rows(2).Cells(2).Range.ContentControls(1).Range.Text / CLng(ContentControl.Range.Text) Else oTbl.Rows(lngCount + 3).Cells(2).Range.ContentControls(1).Range.Text = "$0.00" End If Next lngCount oTbl.Range.Fields.Update oTbl.Rows(4).Cells(2).Range.ContentControls(1).Range.Select ActiveDocument.Protect wdAllowOnlyFormFields, True End Select End Sub |
#4
|
|||
|
|||
Solved
Ok, i checked at your table, and got the answer.
Thank you so much. You made my life much simple. |
#5
|
|||
|
|||
You're welcome. I'm sorry but I didn't see your follow on question until just now. I used a Seq field which I think you have figured out for yourself.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Form auto fill based on a different field value. | stct | Word | 26 | 06-15-2019 04:08 PM |
VBA to automatically sort items based on Mail Merge field | taylorblu | Word VBA | 3 | 09-08-2014 09:52 AM |
MACRO - Insert row based on Form Field Criteria | Elan05 | Word VBA | 5 | 04-16-2013 06:39 AM |
Is there a way to do this? (automatically enter text based on form data) | TIKKI555 | Word | 0 | 05-26-2010 09:21 AM |
Form field to automatically be added to header? | razberri | Word VBA | 3 | 02-22-2010 03:48 PM |