简体   繁体   English

如何从 Excel 获取数据并使用 MailMerge 将其合并到 Word 中?

[英]How to get data from Excel and merge it into Word using MailMerge?

Trying to get data from Excel and merge it into Word using MailMerge (just like how it is done in this video ).尝试从 Excel 获取数据并使用 MailMerge 将其合并到 Word 中(就像在此视频中所做的那样)。 However, fields aren't getting updated after running this code.但是,运行此代码后,字段不会更新。 VBA isn't throwing any error so looks like code is fine. VBA 没有抛出任何错误,所以看起来代码很好。 Can you please help?你能帮忙吗?

Sub getdata()
Dim numRecord As Integer
Dim myName As String

myName = InputBox("Enter the field name and relax!")
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:=myName, Field:="Fields") = True Then
    numRecord = dsMain.ActiveRecord
End If
End Sub

Note: Data in Excel looks like this:注意:Excel 中的数据如下所示:

Fields  First Layer    Second Layer
 CC         5              3

So when someone enters CC in Input box I want first_layer and Second_layer fields in word to get updated to 5 and 3 respectiely.因此,当有人在输入框中输入 CC 时,我希望 word 中的 first_layer 和 Second_layer 字段分别更新为 5 和 3。

If you're running the mailmerge from Word, you don't actually need any VBA for this - it can all be done with a SKIPIF field.如果您从 Word 运行邮件合并,则实际上不需要任何 VBA - 这一切都可以通过 SKIPIF 字段完成。 For example the following field code does the same as the macro in the video is supposed to:例如,下面的域代码与视频中的宏应该做的一样:

{SKIPIF{FILLIN "Name to merge" \o}<> {MERGEFIELD Name}} {SKIPIF{FILLIN "要合并的名称" \o}<> {MERGEFIELD 名称}}

or:或者:

{SKIPIF{FILLIN "Name to merge" \o}<> «Name»} {SKIPIF{FILLIN "要合并的名称" \o}<> «名称»}

Note: The field brace pairs (ie '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you're using a laptop, you might need to use Ctrl-Fn-F9);注意:上面示例的字段大括号对(即'{}')都是在文档本身中创建的,通过 Ctrl-F9(Mac 上的 Cmd-F9,或者,如果您使用的是笔记本电脑,您可能需要使用 Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message.您不能简单地键入它们或从该消息中复制和粘贴它们。 Nor is it practical to add them via any of the standard Word dialogues.通过任何标准的 Word 对话添加它们也不实际。 Likewise, the chevrons (ie '« »') are part of the actual mergefields - which you can insert from the 'Insert Merge Field' dropdown (ie you can't type or copy & paste them from this message, either).同样,人字形(即“« »”)是实际合并字段的一部分——您可以从“插入合并字段”下拉列表中插入(即您也不能从该消息中键入或复制和粘贴它们)。 The spaces represented in the field constructions are all required.字段结构中表示的空格都是必需的。

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

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