You are currently offline, waiting for your internet to reconnect

Text form fields are not retained during mail merge in Word

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q286841

For a Microsoft Word 2000 version of this article, see 211308.
SYMPTOMS
In Microsoft Office Word, when you create a mail-merge document, and the mail-merge main document contains form fields, the main document retains the drop-down and check box form fields, but text form fields are not visible in the merged document as expected.
CAUSE
This behavior occurs because 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.
WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to 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 requirements.

Work around this issue by using the following sample macros. These sample macros perform the following steps:
  1. Replace text form fields in the main mail-merge document with placeholders while preserving the contents of the form fields.
  2. Perform a 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 before running the macro.
IMPORTANT NOTE: These two macros work in conjunction with one another, and both macros must be created in the same Microsoft Visual Basic for Applications project. The first macro calls the second macro.

Macro 1

Use one of the following macro examples, as appropriate for your situation.

If you want to merge to a new document, use Example 1. If you want to merge to e-mail recipients, use Example 2.

Example 1: Merge to a New Document

Sub PreserveMailMergeFormFieldsNewDoc()Dim fFieldText() As StringDim iCount As IntegerDim fField As FormFieldDim sWindowMain, sWindowMerge As StringOn 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.UnprotectEnd 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 IfNext aField' Perform mail merge to new document.ActiveDocument.MailMerge.Destination = wdSendToNewDocumentActiveDocument.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).ActivateErrHandler:End Sub				

Example 2: Merge to E-mail Recipients

Sub PreserveMailMergeFormFieldsEmail()Dim fFieldText() As StringDim iCount As IntegerDim fField As FormFieldDim sWindowMain, sWindowMerge As StringOn 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.UnprotectEnd 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      ' inserts the result      Selection.TypeText afield.Result      ' Increment icount      iCount = iCount + 1   End IfNext afield' Perform mail merge to Email.ActiveDocument.MailMerge.Destination = wdSendToEmail' If you replace Mail as Attachment to true then the attached' document will contain values from the form fieldActiveDocument.MailMerge.MailAsAttachment = False' the field testfield is the email address field name - it may need<BR/>' to be changed, depending on the data source field namesActiveDocument.MailMerge.MailAddressFieldName = "testfield"ActiveDocument.MailMerge.MailSubject = "New MM  macro as attachment "ActiveDocument.MailMerge.ExecuteErrHandler:End Sub				

Macro 2

Also create the following sample macro as the next step in working around the issue described in the "Symptoms" section:
Sub doFindReplace(iCount As Integer, fField As FormField, _   fFieldText() As String)' Go to top of document.Selection.HomeKey Unit:=wdStory' Initialize Find.Selection.Find.ClearFormattingWith 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   NextEnd WithEnd Sub				
MORE INFORMATION
To create and run a macro in Word, follow these steps:
  1. Start Word.
  2. Open the template or document in which you want to store the macro or macros.
  3. Press ALT+F11 to start the Visual Basic Editor.
  4. On the Insert menu, click Module.
  5. In the module sheet, type the macro code.
  6. Press ALT+F11 to return to Word.
  7. On the Tools menu, point to Macro, and then click Macros.
  8. In the Macro name list, click the macro that you want, and then click Run.
prb WD2002 WD2003
Properties

Article ID: 286841 - Last Review: 01/29/2007 13:41:22 - Revision: 2.2

  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
  • kbprb KB286841
Feedback