Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word mailmerge not filling var in footers.

Status
Not open for further replies.

robinr

Technical User
Jan 15, 2003
5
US
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
 
I'm kind of in a hurry so I just pasted some code I used related to headers and footers. I had to use .SeekView=wdSeekCurrentPageHeader to switch to header and footer view. Not exactly a mailmerge but a search and replace... hope this helps

======================
' Now Tweak the Date and Name in the Page 2 Header and Footer
With MSWord
.Selection.HomeKey Unit:=6 ' 6=wdStory
MSWord.ActiveWindow.ActivePane.View.SeekView = 9 ' 9= wdSeekCurrentPageHeader
MSWord.ActiveWindow.ActivePane.View.NextHeaderFooter

MsWord.Selection.Find.Execute FindText:="<TODAY>", Replace:=2, ReplaceWith:=Format(Now(), "Mmmm dd, yyyy") ' 2=wdReplaceAll

' Now back to the main document
MSWord.ActiveWindow.ActivePane.View.SeekView = 0 ' 0 = wdSeekMainDocument
 
Hi Robinr,

I stumbled across your post this evening and it solved a problem I've been battling with for hours. Thanks.

Did you ever figure out how to get around the header/footer problem? I notice that the same thing applies to textboxes which puts a bit of a dent in things.

I'd really appreciate it if you could respond either way.

Thanks.
 
No, I did not resolve the problem with this - I learned from it though, so it was good to have the information. I still can't seem to get the variables that are in the headers and footers to get merged. Any additional help would be great!
 
Hi Robinr,

Managed to get it figured out this morning. The following code works with fields in the header/footer as well as textboxes. Yesterday was the first time that I have ever tried working with Word from VB so if you notice anything in my code that could be improved upon please let me know.

Once again, thanks for posting your code. I would never have figured this out on my own.

Dim oSR As Word.Range
Dim oFld As Word.Field
Dim oRange As Word.Range
Dim szField() As String
Dim lState As Long

For Each oSR In oDoc.StoryRanges
For Each oFld In oSR.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.
szField = Split(oFld.Code, " ")

' if for some reason a code is blank, we need to skip it...
If Trim(szField(2)) = "" Then
GoTo GET_NEXT_FIELD
End If

' this causes the system to select the field in the document
oFld.Select

' put the selected field into a var we can work with
Set oRange = oWord.Selection.Range

' now, retrieve the field data from the XML text using the field name
' we got above in the split statement.
' oRange.Text = "<*!PROBLEM WITH VAR!*>"

On Error GoTo GET_NEXT_FIELD

'TODO get rid of the select statement by passing a collection of FldName/FldData objects to the routine
Select Case szField(2)
Case "FBRecipientCompany"
oRange.Text = szRecipientCompany

Case "FBRecipientAddress"
oRange.Text = szRecipientAddress

Case "FBRecipientName"
oRange.Text = szRecipientName

Case "FBRecipientFirstName"
oRange.Text = szRecipientFirstName

Case "FBSenderCompany"
oRange.Text = szSenderCompany

Case "FBSenderCpyAddress"
oRange.Text = szSenderCpyAddress

Case "FBSenderCpyTel"
oRange.Text = szSenderCpyTel

Case "FBSenderCpyFax"
oRange.Text = szSenderCpyFax

Case "FBSenderName"
oRange.Text = szSenderName

Case "FBSenderTitle"
oRange.Text = szSenderTitle

Case "FBSenderInitials"
oRange.Text = szSenderInitials

Case Else
GoTo GET_NEXT_FIELD
End Select

GET_NEXT_FIELD:
On Error GoTo 0
' go to the next one...
Next 'oFld
Next 'oSR

'TODO see if there is a cleaner way to do the following

'the previous shenanigans cause the document to be left in normal layout view
'with the various sections of the document open - very ugly
'so switch to print layout view
oDoc.ActiveWindow.View = wdPrintView

'now header/footer view is on so make the main document active
oDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

'but that puts us back in normal view so once again switch to print layout view
oDoc.ActiveWindow.View = wdPrintView

'TODO try to make the whole process look a bit smoother - at the moment you can
'see the fields being populated and the document view changing - looks a bit
'messy
 
It works! It works! bcarney - it is a good think you are not here because I would kiss you square on the face!!! I can't tell you how many hours I have trying to find the answer to this problem! Thank you for responding to my post. And by the way - your code looks good.
 
You are most welcome sir, and if you don't mind I'll just accept that kiss "virtually".

Robinr, if you find a way to prevent all the gyrations Word subjects the user to while populating the fields and changing the views, please post it. I'll do likewise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top