Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / November 2004

Tip: Looking for answers? Try searching our database.

Mail Merge fields in Form Text field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vimal - 03 Nov 2004 03:17 GMT
Hi,

Am trying to generate legal documents as part of a financial workflow
application and am using Word mailmerge for this purpose.

Am performing the mail merge by setting a tab delimited text file and
invoking the execute method. Everyhting seemed to work fine until I
noticed that Form Text fileds would not appear in the merged
documents. Thanks to newsgroups I've figured out this issue and fixed
it by using placeholders. However, am still facing the following
problems in this regard:

1. Some of the Form Text fields do not have a bookmark name, because
of which the code fails when I try to search and replace the
placeholders with form text fields in the merged documents. Since I
have about 180 templates, its really cumbersome to go through all
templates and check all Form Text fields for bookmarks. Is there some
easy and efficient way of getting around this problem. Also, a bracket
is displayed against the Form Text field if there is a bookmark for
that field. Is there any way to avoid this?

2. Secondly, merge fields in Form Text fields are not populated during
mail merge. Any inputs?

3. I have a single data source using which I have to merge multiple
different documents. Is there any efficient way of doing this apart
from looping through a collection and merging one document at a time.

Please help with these issues. Thanking you in advance.

Vimal

The following is the code I have:

Set objBaseTemplate = objWordApp.Documents.Add(Template:=strFileName,
Visible:=True)
   
   objBaseTemplate.ActiveWindow.Visible = True
   
   objBaseTemplate.MailMerge.OpenDataSource strDataSource
   
   objBaseTemplate.MailMerge.Destination = wdSendToNewDocument
   
   objBaseTemplate.MailMerge.SuppressBlankLines = True
     
   For Each frmField In objBaseTemplate.FormFields
       If frmField.Type = wdFieldFormTextInput Then

           ' Redim array to hold contents of text field.
             ReDim Preserve strFieldText(1, iCount + 1)
       
             ' Place content and name of field into array.
             strFieldText(0, iCount) = frmField.Result
             strFieldText(1, iCount) = frmField.Name
       
             ' Select the form field.
             frmField.Select
       
             ' Replace it with placeholder text.
             objBaseTemplate.Application.Selection.TypeText "<" &
strFieldText(1, iCount) & "PlaceHolder>"
       
             ' Increment icount
             iCount = iCount + 1
             blnFormFieldsFlag = True
       End If
   Next frmField
   
   objBaseTemplate.MailMerge.Execute
   
   'doFindReplace iCount, frmField, strFieldText()
   objBaseTemplate.Saved = True
   objBaseTemplate.Close
   Set objBaseTemplate = Nothing
       
   Set objGenDoc = objWordApp.ActiveDocument
   
   If blnFormFieldsFlag = True Then
       doFindReplace iCount, frmField, strFieldText(), objGenDoc
   End If

   objGenDoc.Protect wdAllowOnlyFormFields, True, strPassword
   objGenDoc.SaveAs FileName:=strDocToGenPath

The code in doFindReplace

Private Function doFindReplace(iCount As Integer, fField As FormField,
fFieldText() As String, ByRef objWordDoc As Word.Document) As Boolean

   Dim intLoop                 As Integer
   Dim strFunction             As String
   
   strFunction = "doFindReplace"

   ' Go to top of document.
   objWordDoc.Application.Selection.HomeKey Unit:=wdStory
   
   ' Initialize Find.
   objWordDoc.Application.Selection.Find.ClearFormatting
   
   With objWordDoc.Application.Selection.Find
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
   
      ' Loop form fields count.
       For intLoop = 0 To iCount
   
         ' Execute the find.
         Do While .Execute(FindText:="<" & fFieldText(1, intLoop) _
            & "PlaceHolder>") = True
   
            ' Replace the placeholder with the form field.
            Set fField =
objWordDoc.Application.Selection.FormFields.Add _
               (Range:=objWordDoc.Application.Selection.Range,
Type:=wdFieldFormTextInput)
   
            ' Restore form field contents and bookmark name.
            fField.Result = fFieldText(0, intLoop)
            fField.Name = fFieldText(1, intLoop)
         Loop
   
         ' Go to top of document for next find.
         objWordDoc.Application.Selection.HomeKey Unit:=wdStory
   
      Next
   End With
   
   doFindReplace = True
   Exit Function
End Function
Cindy M  -WordMVP- - 04 Nov 2004 12:00 GMT
Hi Vimal,

> 1. Some of the Form Text fields do not have a bookmark name, because
> of which the code fails when I try to search and replace the
[quoted text clipped - 4 lines]
> is displayed against the Form Text field if there is a bookmark for
> that field. Is there any way to avoid this?

You should find an article on the word.mvps.org website by "Ibby" that
describes how to assign names to existing form fields that don't have
any.

Tools/Options/View: there's a checkbox for bookmarks. Deactivating it
should hide the brackets.

> 2. Secondly, merge fields in Form Text fields are not populated during
> mail merge. Any inputs?

I doubt very much that this is possible. How would they even get in
there, in the first place? They certainly can't be inserted from the
interface when the document is in protected mode. Anyway, the document
has to be unprotected to execute the merge. As soon as an unprotected
form field is updated, the content is lost. That's in the KB article
you must have looked at. I'm afraid you'd need another kind of
"placeholder", here, to feed the data into after the merge has
executed.

> 3. I have a single data source using which I have to merge multiple
> different documents. Is there any efficient way of doing this apart
> from looping through a collection and merging one document at a time.

No.

It sounds to me as if you've reached a point where you need to ask
yourself whether mail merge makes any sense, or if you shouldn't better
be automating the entire process...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.