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 professionals 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 a Microsoft Certified Partner or the Microsoft fee-based
consulting line at (800) 936-5200. For more information about Microsoft Certified
Partners, please visit the following Microsoft Web site:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
For more information about how to use the sample code in this article, click
the article number below to view the article in the Microsoft Knowledge
Base:
212536
OFF2000: How to Run Sample Code from Knowledge Base Articles
You can work around the problem by using the following sample macros to:
- Replace text form fields in the mail merge main document with placeholders while preserving the contents of the form fields.
- Merge to a new document.
- Replace the placeholders with text form fields and restore the contents of the form fields.
- 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
Create one of the following macro examples appropriate for your situation.
For example, 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: Merging to a New Document
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
Example 2: Merging to E-mail Recipients
Sub PreserveMailMergeFormFieldsEmail()
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
' inserts the result
Selection.TypeText afield.Result
' Increment icount
iCount = iCount + 1
End If
Next 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 field
ActiveDocument.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 names
ActiveDocument.MailMerge.MailAddressFieldName = "testfield"
ActiveDocument.MailMerge.MailSubject = "New MM macro as attachment "
ActiveDocument.MailMerge.Execute
ErrHandler:
End Sub
Macro 2
Also create the following macro example as your second macro to work around the problem described in this article:
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