In the following example a user wished to provide a variable number of fields and associated formatted text dependant on a figure input into a form field, as in the following illustration. For the purpose of the exercise, the control field 'Mortgages' was set as a number field, the following macro was run on exit from that field, and the section to be repeated was bookmarked with the name 'bMortgage'. Note that some of the text is underlined, some is emboldened and some both underlined and emboldened. There are eight fields in the selection, each of which will be provided with a new bookmark name. The bookmark names of the fields in that marked section will be replaced by the macro, with the names "MortTextn" where 'i' is a numeric digit from 1 to 8. Subsequent iterations will be numbered from 8 - 16, 16 -24 etc. The user may return to the Mortgages field and change the number and the number of entries will be readjusted to take account of that changed number. | |
Note: | In the illustration, bookmarks are displayed to aid understanding. |
| |
Sub Mortgages() Dim sNum As Long Dim oRng As Range Dim bProtected As Boolean Dim fCount As Long Dim i As Long Dim j As Long 'Unprotect the file If ActiveDocument.ProtectionType <> wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If sNum = ActiveDocument.FormFields("Mortgages").Result Selection.GoTo What:=wdGoToBookmark, Name:="bMortgage" 'Define the range object. This is where the cursor is at (start point) Set oRng = Selection.Range For i = 1 To sNum With Selection 'Insert two blank lines .TypeParagraph .TypeParagraph 'Set the underline option .Font.Underline = wdUnderlineSingle 'Set the bold option .Font.Bold = False 'Ensure that the paragraphs stay together on the same page .ParagraphFormat.KeepWithNext = True .TypeText "Plaintiff: " .Font.Underline = wdUnderlineNone 'Add a form field .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeParagraph .TypeParagraph .Font.Underline = wdUnderlineSingle .Font.Bold = True .TypeText "Mortgage:" 'Turn off the underline .Font.Underline = wdUnderlineNone 'Turn off the bold option .Font.Bold = False .TypeParagraph .TypeParagraph .TypeText "From: " .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeParagraph .TypeParagraph .TypeText "To: " .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeParagraph .TypeParagraph .Font.Bold = True .TypeText "Dated: " .Font.Bold = False .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeText Chr(32) 'Add a Space .Font.Bold = True .TypeText "Recorded: " .Font.Bold = False .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeText Chr(32) .Font.Bold = True .TypeText "OR Book: " .Font.Bold = False .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeText Chr(32) .Font.Bold = True .TypeText "Page: " .Font.Bold = False .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput .TypeText Chr(32) .Font.Bold = True .TypeText "Original Amount: $" .Font.Bold = False .FormFields.Add Range:=Selection.Range, Type:=wdFieldFormTextInput 'Turn off the Keep with next option .ParagraphFormat.KeepWithNext = False .TypeParagraph .TypeParagraph End With Next i 'The selection has moved down the page. Redefine the end of the range object. oRng.End = Selection.Range.End 'Recreate the bookmark. ActiveDocument.Bookmarks.Add "bMortgage", oRng 'Count the form fields added to the range fCount = oRng.FormFields.Count 'Give each of the added fields a unique bookmark name For j = 1 To fCount With oRng.FormFields(j) .Name = "MortText" & j 'Add a unique bookmark name .Enabled = True 'Enable the field for user entry .CalculateOnExit = False 'Uncheck the calculate on exit check box End With Next j 'Reprotect the document. If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, _ NoReset:=True, _ Password:="" End If 'Select the first of the fields in the range ActiveDocument.FormFields("MortText1").Select End Sub |
Thursday, September 8, 2011
Repeat a block of formatted text and form fields based upon the content of another form field
Labels:
MACROS
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment