I have inserted a field {FILENAME} in the footer of my source document.
When I generate a MailMerge with this source document and a data file the resulting document does not show the field code - it has been replaced by hardcoded text representing the current document's filename - 'Document1'. This is the code to show that I am not doing anything particularly fancy:
Call vActiveDocument.MailMerge.OpenDataSource(vDataSourcePath)
vActiveDocument.MailMerge.SuppressBlankLines = True
vActiveDocument.MailMerge.Destination = 0 'Send to new Document
Call vActiveDocument.MailMerge.execute(True)
Call vActiveDocument.Close(False)
Set vActiveDocument = vApplication.activedocument
Am I missing something? I am expecting the code field to remain a code field, even after the mailmerge operation.Is there a way to tell Word 'calculate the MERGEFIELD fields, but do not calculate the other form fields'.
At the moment I am using a clunky search and replace but this is really ugly. ugly ugly ugly. Could even qualify as a hack.
'//get current filename
fileName = vActiveDocument.Name
'//check if we need to replace foooter
If ( replaceFileNameInFooter) then
'//Replacing current document filename with a computed field
'//set view to footer
vApplication.ActiveWindow.ActivePane.View.SeekView = 10
'//Assing footer
Set footer = vApplication.Selection.Range
'//search for current filename -> example: FormLetters1
footer.Find.Text = filename
'//replace with a filename field -> Type 29
While footer.Find.Execute()
Call vApplication.Selection.Fields.Add(footer, 29)
Wend
'//set main document mode
vApplication.ActiveWindow.ActivePane.View.SeekView = 0
End if
As far as I know, the only way you can do this using the field language is another hack, and it's probably an even more difficult one to manage.
Put the field code you want (e.g. { FILENAME \p } in another document, bookmark it, e.g. using "bm_filename_p", then use
{ INCLUDETEXT "the full path+name of the document" bm_filename_p }
in your Mail Merge Main Document.
Post-merge, all the INCLUDETEXT fields are retained, but you will probably need to save the file then update the fields in the footer to get the result you want. At that point, if you want to "fix" the field results you can do it in the usual way (replace the field codes by their results), but if you do not do that, you will always need the included file to be in the specified location.
Although that works with FILENAME, and it will even work with a field such as { SEQ abc } in the body of the Mail Merge Main Document, it doesn't work, for example with { SEQ abc \c } in the footer.
One way you may be able to make the search and replacement task a little easier is to insert a { PAGE } field with an identifier, like this
{ PAGE \#field_filename }
These fields are also retained post-merge and it may be slightly easier to find/replace them (e.g. look for { PAGE } fields and replace "PAGE #field_" with "" , or some such.