我在源文档的页脚中插入了一个字段 {FILENAME}。
当我使用此源文档和数据文件生成 MailMerge 时,生成的文档不显示域代码 - 它已被表示当前文档文件名的硬编码文本替换 - “Document1”。这是表明我没有做任何特别花哨的代码:
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
我错过了什么吗?我希望代码字段仍然是代码字段,即使在邮件合并操作之后也是如此。有没有办法告诉 Word '计算 MERGEFIELD 字段,但不计算其他表单字段'。
目前我正在使用笨重的搜索和替换,但这真的很难看。丑丑丑。甚至可以称为黑客。
'//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