V
Vimal
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
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