使用 VBA 更改 Word MailMerge 字段的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15824115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:26:44  来源:igfitidea点击:

Change value of Word MailMerge field using VBA

vbams-wordword-vbamailmerge

提问by David

I have a document with MailMerge fields, however I do not want to use the whole datasource->mailmerge idea. Instead I am presenting a UserForm on Autonew() and asking the user to type the data into the fields.

我有一个带有 MailMerge 字段的文档,但是我不想使用整个 datasource->mailmerge 的想法。相反,我在 Autonew() 上展示了一个用户表单,并要求用户在字段中输入数据。

The reason is because this document is to "merged" with one row of data so it is a waste of time asking the user to do the whole data source thing for one row.

原因是因为这个文档是与一行数据“合并”的,所以要求用户为一行做整个数据源的事情是浪费时间。

I have a working solution using DocVariables which I'm sure most people will say is the correct way to do it, however this client likes the idea of "seeing" the mailmerge variable (such as <<1>>) in the source document. They know they can use Alt-F9 to display codes to see the DocVariables, but they insist on wanting to use MergeFields.

我有一个使用 DocVariables 的工作解决方案,我相信大多数人会说这是正确的方法,但是这个客户喜欢在源文档中“看到”邮件合并变量(例如<<1>>)的想法. 他们知道他们可以使用 Alt-F9 来显示代码以查看 DocVariables,但他们坚持要使用 MergeFields。

Using DocVariables I do it using the following. This works so I know I have the right idea and that the rest of my code works fine.

使用 DocVariables 我使用以下方法进行操作。这有效,所以我知道我有正确的想法,并且我的其余代码工作正常。

ActiveDocument.Variables("varSurame").Value = .txtSurname

However, I cannot workout how to do the same thing with merge fields. I want to do something like the below (but there is no "value" property to set).

但是,我无法练习如何对合并字段执行相同的操作。我想做类似下面的事情(但没有要设置的“值”属性)。

ActiveDocument.MailMerge.Fields("Surname").value = .txtSurname

The .text property is readonly so I cannot use that.

.text 属性是只读的,所以我不能使用它。

The following renders "Bookmark not defined" errors.

以下呈现“书签未定义”错误。

ActiveDocument.MailMerge.Fields("Surname").Code.Text = .txtSurname

Any ideas on how I can programmatically change the value of a mail merge field without using a datasource.

关于如何在不使用数据源的情况下以编程方式更改邮件合并字段的值的任何想法。

回答by

Something like:

就像是:

Dim f as Word.Field

For Each f in ActiveDocument.Fields

  If f.Type = wdFieldMergeField Then

    ' you will either need to extract the name of the field from f.code here
    ' (roughly speaking, you should expect to find
    ' MERGEFIELD<white space><optional double quote>fieldname<optional double quote><white space><possible switches>
    ' or you could iterate through a list of field names and use instr to look for each name
    ' then

    f.Result.Text = "the text you want"
  End If
Next ' f

回答by JRL

I had a Word Document that I wanted to have DocVariable fields that I could modify from Access via VBA. I too wanted to "see" the mail merge fields. If I created the document with DocVariable fields to start with (e.g.,, Insert>>Quick Parts>>Field and the insert a DocVariable field), they were either invisible or displayed the entire long field code. So suppose I have fields named FNAMEand DONATIONin a letter thanking someone for their donation as the document.

我有一个 Word 文档,我想要它有 DocVariable 字段,我可以通过 VBA 从 Access 修改它。我也想“查看”邮件合并字段。如果我使用 DocVariable 字段创建文档(例如,插入>>快速部件>>字段并插入一个 DocVariable 字段),它们要么不可见,要么显示整个长字段代码。所以假设我有命名的字段,FNAMEDONATION在一封感谢某人捐赠的信中作为文档。

The final document should look like:

最终文件应如下所示:

...Thank you Bob for your donation of $100. It will...

...感谢鲍勃捐赠 100 美元。它会...

With the DocVariable fields, it either looked like this:

对于 DocVariable 字段,它看起来像这样:

...Thank you for your donation of . It will...

...感谢您的捐赠。它会...

or like this:

或者像这样:

...Thank you { DOCVARIABLE "FNAME"} for your donation of { DOCVARIABLE "DONATION"}. It will...

...感谢 { DOCVARIABLE "FNAME"} 捐赠 { DOCVARIABLE "DONATION"}。它会...

or possibly even longer if the DocVariable fields have things like `* MERGEFORMAT' in them. It might be silly, but I definitely understand the preference for "seeing" the mail merge field, so that it looks like this

或者甚至更长,如果 DocVariable 字段中有类似 `* MERGEFORMAT' 的内容。这可能很愚蠢,但我绝对理解“看到”邮件合并字段的偏好,所以它看起来像这样

...Thank you ?FNAME? for your donation of ?DONATION?. It will...

...谢谢你?FNAME?为您的捐赠?捐赠?。它会...

The way I got it to work was this:

我让它工作的方式是这样的:

  1. Create the document with mail merge fields. You might have to make a fake Excel file or database to connect to in order to add the fields. After doing this, you'll "see" the mail merge fields in the document (e.g., ?FNAME?).
  2. Toggle the field codes (alt-F9) so you see { MERGEFIELD "FNAME"}.
  3. For each mail merge field, replace the "MERGEFIELD" with "DOCVARIABLE". Just type right over it so that it looks like `{ DOCVARIABLE "FNAME"}.
  4. Toggle the field codes again (alt-F9). It will look like a mail merge field (e.g., `?FNAME?') but you can use it in VBA like a DocVariable field.
  1. 创建包含邮件合并域的文档。您可能需要制作一个虚假的 Excel 文件或数据库来连接以添加字段。执行此操作后,您将“看到”文档中的邮件合并字段(例如,?FNAME?)。
  2. 切换域代码 (alt-F9),以便您看到{ MERGEFIELD "FNAME"}.
  3. 对于每个邮件合并字段,将“MERGEFIELD”替换为“DOCVARIABLE”。只需在它上面键入,使其看起来像`{ DOCVARIABLE "FNAME"}.
  4. 再次切换域代码 (alt-F9)。它看起来像一个邮件合并字段(例如,`?FNAME?'),但您可以在 VBA 中像使用 DocVariable 字段一样使用它。

Note that you can actually just insert one mail merge field and then copy/paste it, changing the name for different fields. There must be something hidden in the mail merge field that gets copied when you do this that doesn't exist if you just insert a DocVariable field, but I haven't been able to figure out what it is. So this is definitely a workaround, but it worked for me. It gives you the best of both worlds, "seeing" the mail merge field but getting to use it like a DocVariable field from VBA.

请注意,您实际上可以只插入一个邮件合并字段,然后复制/粘贴它,更改不同字段的名称。执行此操作时,邮件合并字段中一定有一些隐藏的东西会被复制,如果您只是插入一个 DocVariable 字段,则该字段不存在,但我一直无法弄清楚它是什么。所以这绝对是一种解决方法,但它对我有用。它为您提供了两全其美的方法,“看到”了邮件合并字段,但可以像使用 VBA 中的 DocVariable 字段一样使用它。