1

我在源文档的页脚中插入了一个字段 {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
4

1 回答 1

0

据我所知,使用现场语言做到这一点的唯一方法是另一种 hack,而且它可能更难管理。

将您想要的域代码(例如 { FILENAME \p } 在另一个文档中,为其添加书签,例如使用“bm_filename_p”,然后使用

{ INCLUDETEXT "the full path+name of the document" bm_filename_p }

在您的邮件合并主文档中。

合并后,保留所有 INCLUDETEXT 字段,但您可能需要保存文件,然后更新页脚中的字段以获得所需的结果。此时,如果您想“修复”字段结果,您可以按照通常的方式进行(用结果替换字段代码),但如果您不这样做,您将始终需要包含的文件指定的位置。

虽然这适用于 FILENAME,它甚至适用于邮件合并主文档正文中的 { SEQ abc } 等字段,但它不起作用,例如页脚中的 { SEQ abc \c }。

您可以使搜索和替换任务更容易一些的一种方法是插入一个带有标识符的 { PAGE } 字段,如下所示

{ PAGE \#field_filename }

这些字段也在合并后保留,查找/替换它们可能会稍微容易一些(例如,查找 { PAGE } 字段并将 "PAGE #field_" 替换为 "" 或类似的。

于 2014-05-08T17:05:48.727 回答