• merging into a form with text fields (97/SR1)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » merging into a form with text fields (97/SR1)

    Author
    Topic
    #374059

    We have a form with text fields, and need to perform a mail merge with it. Of course, we have had to unprotect the form in order to perform the merge, which then works fine. However, once merged, the text fields have disappeared. Is there a solution to this? In other words to keep the text fields AND also perform a merge???

    Viewing 2 reply threads
    Author
    Replies
    • #603858

      You could try building the whole thing with a macro. We have a letter that uses a mailmerge from an Access database and writes the letter text as it goes. If you were to record a macro adding the fields you could add this in at the end of the merge.

      I have attached our letter macro to this post. It’s very specialised but it does 2 merges and inserts mailmerge fields so you might be able to make use of bits of it. Very clumsy scripting I’m afraid but I’m still on the learning curve.

      David

    • #604632

      I’m not sure I can help, as I’m a little unclear as to what you’re doing. When you say you need to perform a mail merge with your form, are you using the form as a main document or as a datasource? If the latter, could you be more specific as to how it’s laid out. Is there a table in it with variable data? Perhaps you could attach a sample to your post.
      Cheers,

      • #604730

        Phil – The form is the main document and has text fields in it. Once merged (with thousands of data records), the text fields remaining are to be filled in on an individual basis. Hope this clarifies.

        • #604781

          You can’t merge form fields. That is, when you perform the merge, the form fields themselves are not merged, so merely trying to reprotect the form without resettting form fields shouldn’t make a difference–there are no form fields left in the merged document. However, I copied the following tip from somewhere (I thought it was MS KB, but I’m not sure). Hope this helps.

          MailMerge: Text Form Fields Are Not Retained
          Word unlinks the text form fields during the mail merge. Word does not unlink display form fields, such as the drop-down and the check box form fields.

          You can work around the problem by using the following sample macros to:

          1. Replace text form fields in the main mail merge document with placeholders while preserving the contents of the form fields.
          2. Merge to a new document.
          3. Replace the placeholders with text form fields and restore the contents of the form fields.
          4. Restore the main mail merge document to its original content prior to running the macro.

          NOTE: These two macros work in conjunction with one another, and both macros must be created in the same Visual Basic for Applications project. The first macro will call the second macro.

          Macro 1:

          Sub PreserveMailMergeFormFields()
             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 ""
          
                   ' 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

          Macro 2:

          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:="") = 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
    • #604708

      If you want to keep the values in the FormFields after you unprotect the form, you will need to run a macro to reprotect the Form. It only needs to be 1 line:

      ActiveDocument.Protect Type, NoReset, Password

      There are 3 parameters to the Protect Method.
      Type – is required and indicates the type of Protection (wdAllowOnlyFormFields to Protect for Forms)

      NoReset – is optional and indicates if the FormFields should be reset to default values or retain their current value (True = Retain Current Value False = Reset to default)

      Password – is optional and is the protection password

      So assuming that you do not have a password on you form, the actual line would look like:

      ActiveDocument.Protect wdAllowOnlyFormFields,True

    Viewing 2 reply threads
    Reply To: merging into a form with text fields (97/SR1)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: