Hi Cindy,
Add some lines to the program that will pause execution after
- the search term is found
?Has the search term actually been found in the document (if you're using
Range.Find, stick a range.Select in there so that you can see what's been
found)
I am unable to replicate the problem on my development machines but the code
contains Trace commands that customers can turn on in situations like this.
Below is the relevant code and the Trace output from the customer's machine
appears below that.
As you can see the program tests for a successful find with "If .Find.Found
then". Second and subsequent .Find commands find the original text
indicating that the .Add doesn't change the document.
Set objWordMailMerge = objWordDoc.MailMerge
Set objWordMailMergeFields = objWordMailMerge.Fields
Do ' (while target field is found and no error)
Set objRange = .Content
With objRange
Trace PROCEDURE_NAME & " - Find (1) " & strTargetMergeField
lngErrNumber = 0
On Error Resume Next
.Find.Execute FindText:=strTargetMergeField,
Forward:=True
lngErrNumber = Err.Number
Trace PROCEDURE_NAME & " - After Find (1) " & strTargetMergeField &
IIf(lngErrNumber <> 0, " Error: " & lngErrNumber & " " & Err.Description,
"")
On Error GoTo ErrorHandler
Trace PROCEDURE_NAME & " - After On Error... "
If lngErrNumber <> 0 Then
Trace PROCEDURE_NAME & " - Error No. " & lngErrNumber & " generated from
..Find.Execute FindText:=" & strTargetMergeField
If lngErrNumber = 5590 Then
' Error 5590 - The Find What text
contains a range that is not valid
' Ignore this error - probably
nothing in range
lngErrNumber = 0
Trace "OLE Find " & strTargetMergeField & _
" returned Error 5590 - The Find What text contains a range that is not
valid" & vbCrLf & _
" Content section may not
contain anything to search for"
Else
' Reissue command with error handler
on to force the error again
Trace PROCEDURE_NAME & " - Issuing .Find.Execute FindText:=" &
strTargetMergeField
.Find.Execute
FindText:=strTargetMergeField, Forward:=True
End If
Else
' There was no error number
Trace PROCEDURE_NAME & " - There was no error"
End If
If lngErrNumber = 0 Then
' Either no Error number or Error No.
5590 ignored
Trace PROCEDURE_NAME & " - Before If .Find.Found Then"
If .Find.Found Then
Trace PROCEDURE_NAME & " - After If .Find.Found Then - Found"
.Select
Set objSelection =
objWordApp.Selection
Trace PROCEDURE_NAME & " - Before objWordMailMergeFields.Add " &
strReplacementMergeField
objWordMailMergeFields.Add _
objSelection.Range,
strReplacementMergeField
Trace PROCEDURE_NAME & " - After objWordMailMergeFields.Add " &
strReplacementMergeField
If Not objSelection Is Nothing Then
Set objSelection = Nothing
End If
Else
' Target field not found
Trace PROCEDURE_NAME & " - After If .Find.Found Then - NOT Found - About to
Exit Do"
Exit Do
End If
Else
' There was an error number
End If ' If lngErrNumber = 0 Then
End With ' With objRange
DoEvents
' Check if merge was cancelled
If m_blnProcessCancelled Then
' Merge Cancelled by operator
Trace PROCEDURE_NAME & " - Merge Cancelled by operator"
GoTo ExitProcedure
End If
Loop ' (while target field is found and no error)
TRACE OUTPUT FROM COMPUTER WHEN * * NOT * * LOGGED IN AS
ADMINISTRATOR ------------------------------
frmMain::RemapTemplateMergeFieldsForInternetMail - Set objWordMailMerge =
objWordDoc.MailMerge
frmMain::RemapTemplateMergeFieldsForInternetMail - Find (1) [[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After Find (1)
[[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After On Error...
frmMain::RemapTemplateMergeFieldsForInternetMail - There was no error
frmMain::RemapTemplateMergeFieldsForInternetMail - Before If .Find.Found
Then
frmMain::RemapTemplateMergeFieldsForInternetMail - After If .Find.Found
Then - Found
frmMain::RemapTemplateMergeFieldsForInternetMail - Before
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - After
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - Find (1) [[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After Find (1)
[[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After On Error...
frmMain::RemapTemplateMergeFieldsForInternetMail - There was no error
frmMain::RemapTemplateMergeFieldsForInternetMail - Before If .Find.Found
Then
frmMain::RemapTemplateMergeFieldsForInternetMail - After If .Find.Found
Then - Found
frmMain::RemapTemplateMergeFieldsForInternetMail - Before
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - After
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - Find (1) [[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After Find (1)
[[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After On Error...
frmMain::RemapTemplateMergeFieldsForInternetMail - There was no error
frmMain::RemapTemplateMergeFieldsForInternetMail - Before If .Find.Found
Then
frmMain::RemapTemplateMergeFieldsForInternetMail - After If .Find.Found
Then - Found
frmMain::RemapTemplateMergeFieldsForInternetMail - Before
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - After
objWordMailMergeFields.Add Custom1
}
}
} Lot's more repetition here until user cancels operation
}
}
frmMain::RemapTemplateMergeFieldsForInternetMail - Find (1) [[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After Find (1)
[[NamePrefix]]
frmMain::RemapTemplateMergeFieldsForInternetMail - After On Error...
frmMain::RemapTemplateMergeFieldsForInternetMail - There was no error
frmMain::RemapTemplateMergeFieldsForInternetMail - Before If .Find.Found
Then
frmMain::RemapTemplateMergeFieldsForInternetMail - After If .Find.Found
Then - Found
frmMain::RemapTemplateMergeFieldsForInternetMail - Before
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - After
objWordMailMergeFields.Add Custom1
frmMain::RemapTemplateMergeFieldsForInternetMail - Merge Cancelled by
operator
Regards
Tony Scott