简体   繁体   English

是否可以使用Word宏将一个邮件合并字段替换为另一个?

[英]Is it possible to use a Word macro to replace one mail merge field with another?

I have Word Mail Merge document with merge codes inserted. 我有插入了合并代码的Word Mail Merge文档。 I was wondering if I can write a Word macro to search for one mail merge code and replace it with another? 我想知道是否可以编写Word宏来搜索一个邮件合并代码,然后将其替换为另一个? I have tried, but the merge code I replace reverts back to the original value. 我已经尝试过,但是我替换的合并代码将恢复为原始值。 I create a Word macro and use CTRL + H, put the data in to search and replace for(The merge fields) and click replace. 我创建一个Word宏并使用CTRL + H,将数据放入搜索并替换为(合并字段),然后单击替换。 It replaces the data, but after I save and return to document the old merge code is still there. 它替换了数据,但是在我保存并返回文档之后,旧的合并代码仍然存在。 The following vba code is generated: 生成以下vba代码:

  Selection.Find.ClearFormatting 
  Selection.Find.Replacement.ClearFormatting 
  With Selection.Find 
    .Text = "F_400" 
    .Replacement.Text = "F_901" 
    .Forward = True
    .Wrap = wdFindContinue 
    .Format = False 
    .MatchCase = False 
    .MatchWholeWord = False 
    .MatchWildcards = False 
    .MatchSoundsLike = False 
    .MatchAllWordForms = False 
 End With 
 Selection.Find.Execute

I am replacing merge field F_400 with merge field F_901. 我将合并字段F_400替换为合并字段F_901。

In order to replace the name of a Mergefield you need to work with the underlying field code. 为了替换Mergefield的名称,您需要使用基础域代码。 What you normally see is a "pretty view" presented by Word, that can be switched between "field name" and the data preview provided by the Mailings tab in the Ribbon. 您通常会看到Word提供的“漂亮视图”,可以在“字段名称”和功能区中“邮件”选项卡提供的数据预览之间切换。

In order to use Find/Replace directly on the field codes, use Alt+F9 to toggle to the field code view. 为了直接在域代码上使用“查找/替换”,请使用Alt + F9切换到域代码视图。 You should then see { MERGEFIELD fieldname }. 然后,您应该看到{MERGEFIELD字段名}。 In this state, you can Find fieldname and Replace it with a different, valid field name from the data source. 在这种状态下,您可以查找字段名称并将其替换为来自数据源的其他有效字段名称。

In order to toggle the field codes display as part of a macro so that you can use Find/Replace in VBA, use a Window object with the View.ShowFieldCodes property, for example: 为了切换将域代码显示为宏的一部分,以便可以在VBA中使用“查找/替换”,请使用具有View.ShowFieldCodes属性的Window对象,例如:

ActiveWindow.View.ShowFieldCodes = True 'False to turn them off

This works fine when using Selection.Find. 使用Selection.Find时,此方法工作正常。 For Range.Find there's an alternate approach, by changing the TextRetrievalMode for the Range object being used for the Find: 对于Range.Find,有另一种方法,通过更改用于Find的Range对象的TextRetrievalMode:

rngFind.TextRetrievalMode.IncludeFieldCodes
With rngFind
   'and so on

The IncludeFieldCodes property picks up the "hidden" field codes for the code processing, so it doesn't matter whether the field codes are displayed on-screen, or not. IncludeFieldCodes属性获取用于代码处理的“隐藏”域代码,因此域代码是否显示在屏幕上都没有关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM