Help! Mail merge & using VB code - using form text fields

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mr k
    New Member
    • Feb 2007
    • 2

    Help! Mail merge & using VB code - using form text fields

    Hi,
    I wanted to use mail merge with forms but Text form fields are not retained during mail merge in Word, I got the code from Microsoft but it doesn't remember the text form field options such as the maximum length of the text (which I need) and the text format (would be ideal but can do without if need be)
    I have posted the code below, so please can you help!?? Thanks in advance...


    Code:
    Sub PreserveMailMergeFormFieldsNewDoc()
    
    Dim fFieldText() As String
    Dim iCount As Integer
    Dim fField As FormField
    Dim sWindowMain, sWindowMerge As String
    
    On Error GoTo ErrHandler
    
    ' Store Main merge document window name.
    sWindowMain = ActiveWindow.Caption
    
    ' Because the document contains form fields,
    ' it should be protected, so unprotect document.
    If ActiveDocument.ProtectionType <> wdNoProtection Then
       ActiveDocument.Unprotect
    End If
    
    ' Loop through all text form fields
    ' in the main mail merge document.
    For Each aField In ActiveDocument.FormFields
    
       ' If the form field is a text form field...
       If aField.Type = wdFieldFormTextInput Then
    
          ' Redim array to hold contents of text field.
          ReDim Preserve fFieldText(1, iCount + 1)
    
          ' Place content and name of field into array.
          fFieldText(0, iCount) = aField.Result
          fFieldText(1, iCount) = aField.Name
    
          ' Select the form field.
          aField.Select
    
          ' Replace it with placeholder text.
          Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"
    
          ' Increment icount
          iCount = iCount + 1
    
       End If
    
    Next aField
    
    ' Perform mail merge to new document.
    ActiveDocument.MailMerge.Destination = wdSendToNewDocument
    ActiveDocument.MailMerge.Execute
    
    ' Find and Replace placeholders with form fields.
    doFindReplace iCount, fField, fFieldText()
    
    ' Protect the merged document.
    ActiveDocument.Protect Password:="", NoReset:=True, _
       Type:=wdAllowOnlyFormFields
    
    ' Get name of final merged document.
    sWindowMerge = ActiveWindow.Caption
    
    ' Reactivate the main merge document.
    Windows(sWindowMain).Activate
    
    ' Find and replace placeholders with form fields.
    doFindReplace iCount, fField, fFieldText()
    
    ' Reprotect the main mail merge document.
    ActiveDocument.Protect Password:="", NoReset:=True, _
       Type:=wdAllowOnlyFormFields
    
    ' Switch back to the merged document.
    Windows(sWindowMerge).Activate
    
    ErrHandler:
    
    End Sub
    
    
    Sub doFindReplace(iCount As Integer, fField As FormField, _
       fFieldText() As String)
    
    ' Go to top of document.
    Selection.HomeKey Unit:=wdStory
    
    ' Initialize Find.
    Selection.Find.ClearFormatting
    
    With Selection.Find
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
    
       ' Loop form fields count.
        For i = 0 To iCount
    
          ' Execute the find.
          Do While .Execute(FindText:="<" & fFieldText(1, i) _
             & "PlaceHolder>") = True
    
             ' Replace the placeholder with the form field.
             Set fField = Selection.FormFields.Add _
                (Range:=Selection.Range, Type:=wdFieldFormTextInput)
    
             ' Restore form field contents and bookmark name.
             fField.Result = fFieldText(0, i)
             fField.Name = fFieldText(1, i)
          Loop
    
          ' Go to top of document for next find.
          Selection.HomeKey Unit:=wdStory
    
       Next
    End With
    
    End Sub
    Last edited by willakawill; Feb 15 '07, 08:33 PM. Reason: please use code tags when posting code
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. I have no experience with this process. Having said that, a quick look through your code tells me something you might like :)
    At this point:
    Code:
    ' Redim array to hold contents of text field.
          ReDim Preserve fFieldText(1, iCount + 1)
    
          ' Place content and name of field into array.
          fFieldText(0, iCount) = aField.Result
          fFieldText(1, iCount) = aField.Name
    you can alter the code to accept as many fields of data that you wish as long as you understand that the data is being stored as a string for the time being and you can alter that when you need to later.
    Code:
    ' Redim array to hold contents of text field.
          ReDim Preserve fFieldText(2, iCount + 1)
    
          ' Place content and name of field into array.
          fFieldText(0, iCount) = aField.Result
          fFieldText(1, iCount) = aField.Name
          fFieldText(2, iCount) = 'your max length here
    Hope this helps

    Comment

    Working...