I have an application that gets an XML string via a winsocket, and then uses that string to fill the mailmerge vars in a document. I am using VB6, and Word is version 2000. The merge works perfect EXCEPT when there are footers (or headers) in the document - the mailmerge variables in the footer or header do not get filled (Note: all other variables fill correctly). Any clue why? Following is the main section of code that does the merge:
wrdWord.Visible = True
wrdWord.ScreenUpdating = False
Set wrdWordDoc = wrdWord.Documents.Add(tDocumentTemplate, False)
' convert it back to just a normal word document, not a mailmerge document
wrdWordDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
For Each wrdWordField In wrdWordDoc.MailMerge.fields
' the "wordfield.code" is in the format "MERGEFIELD fieldname". Use the
' SPLIT command (with space as the field delimeter) to put the two values
' into an array, and the merge field name will be in element 2 of the
' array.
tField = Split(wrdWordField.Code, " ")
' if for some reason a code is blank, we need to skip it...
If Trim(tField(2)) = "" Then
GoTo NxtField
End If
' this causes the system to selelect the field in the document
wrdWordField.Select
' put the selected field into a var we can work with
Set wrdWordFieldRange = wrdWord.Selection.Range
' now, retrieve the field data from the XML text using the field name
' we got above in the split statement.
wrdWordFieldRange.Text = "<*!PROBLEM WITH VAR!*>"
On Error GoTo NxtField
Set xXMLFieldNode = xXMLDocNode.selectSingleNode(tField(2))
If xXMLFieldNode Is Nothing Then
GoTo NxtField
End If
' replace the Word field range selected with the value from the XML
wrdWordFieldRange.Text = xXMLFieldNode.Text
' the XML sends a tilde (~) when the field is blank. We need to make
' the tilde's be blank.
If Trim(wrdWordFieldRange.Text) = "~" Then
wrdWordFieldRange.Text = ""
End If
'
NxtField:
On Error GoTo 0
' go to the next one...
Next wrdWordField
wrdWord.Visible = True
wrdWord.ScreenUpdating = False
Set wrdWordDoc = wrdWord.Documents.Add(tDocumentTemplate, False)
' convert it back to just a normal word document, not a mailmerge document
wrdWordDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
For Each wrdWordField In wrdWordDoc.MailMerge.fields
' the "wordfield.code" is in the format "MERGEFIELD fieldname". Use the
' SPLIT command (with space as the field delimeter) to put the two values
' into an array, and the merge field name will be in element 2 of the
' array.
tField = Split(wrdWordField.Code, " ")
' if for some reason a code is blank, we need to skip it...
If Trim(tField(2)) = "" Then
GoTo NxtField
End If
' this causes the system to selelect the field in the document
wrdWordField.Select
' put the selected field into a var we can work with
Set wrdWordFieldRange = wrdWord.Selection.Range
' now, retrieve the field data from the XML text using the field name
' we got above in the split statement.
wrdWordFieldRange.Text = "<*!PROBLEM WITH VAR!*>"
On Error GoTo NxtField
Set xXMLFieldNode = xXMLDocNode.selectSingleNode(tField(2))
If xXMLFieldNode Is Nothing Then
GoTo NxtField
End If
' replace the Word field range selected with the value from the XML
wrdWordFieldRange.Text = xXMLFieldNode.Text
' the XML sends a tilde (~) when the field is blank. We need to make
' the tilde's be blank.
If Trim(wrdWordFieldRange.Text) = "~" Then
wrdWordFieldRange.Text = ""
End If
'
NxtField:
On Error GoTo 0
' go to the next one...
Next wrdWordField