WD97: Text Form Fields Not Retained During Mail Merge

Last reviewed: February 23, 1998
Article ID: Q181577
The information in this article applies to:
  • Microsoft Word 97 for Windows

SYMPTOMS

A mail merge main document that contains form fields retains the drop-down and check box form fields, but text form fields are not present in the merge result.

CAUSE

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. This behavior is by design.

WORKAROUND

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

The following macros will:

  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: The following two macros work in conjunction with one another and both macros must be created in the same Visual Basic for Applications project.

   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 "<" & 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

MORE INFORMATION

Some fields, such as the Next and FillIn fields, require input either from the data document or from you to continue processing the merge. In this case, Word places the results in the merged document. However, fields that obtain information from document statistics, such as the Numpages field, may be inappropriate for a merged document. As a result they are not retained in the merge result.

The following fields are not unlinked during a mail merge:

   = (Formula)
   Advance
   AutoNum
   AutoNumLgl
   AutoNumOout
   BarCode
   Date
   EQ
   FormCheckbox
   FormDropDown
   GotoButton
   IncludePicture
   IncludeText
   Link
   MacroButton
   NoteRef
   Page
   PageRef
   Print
   PrintDate
   Private
   RD
   Section (except in Word for the Macintosh version 6.0 and 6.0.1)
   SectionPages
   Symbol
   TA
   TC
   Time

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q173707
   TITLE     : OFF97: How to Run Sample Code from Knowledge Base Articles

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications


Additional query words: text form mail merge mailmerge lost stripped
removed disappear print merge merging display field formfield drop down
dropdown
Keywords : kbmacroexample word97 kbdta kbdtacode
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbbug
Solution Type : kbpending


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 23, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.