简体   繁体   English

如何通过单击 Excel 工作表中的按钮将邮件合并宏从 excel 使用到 word

[英]How to use mailmerge macro from excel to word with a click of a button in the excel sheet

I am trying to create a code which will be executed with a click of a button in the excel sheet and will ask me for an input of a cell value in excel which will then take all the data from that cell's row and insert it accordingly in the word template where the mail merge tags are.我正在尝试创建一个代码,该代码将通过单击 Excel 工作表中的按钮来执行,并要求我在 excel 中输入一个单元格值,然后从该单元格的行中获取所有数据并相应地将其插入邮件合并标签所在的单词模板。
for example :例如 :
I have data like this in my excel sheet :我的excel表中有这样的数据:

ID(A1), Name(B1), Last Name(C1), Country(D1), Date(E1) ID(A1)、姓名(B1)、姓氏(C1)、国家(D1)、日期(E1)
378232625(A2), John(B2), Smith(C2), United States(D2), 29/02/2020(E2) 378232625(A2), 约翰(B2), 史密斯(C2), 美国(D2), 29/02/2020(E2)
322783145(A3), Joshua(B3), Brand(C3), United States(D3), 27/02/2020(E3) 322783145(A3), Joshua(B3), Brand(C3), 美国(D3), 27/02/2020(E3)

I want to have a button which will ask for an ID input and will then take all the other info of that person and place it in the word template in the correct place.我想要一个按钮,它会要求输入 ID,然后将获取该人的所有其他信息并将其放在 word 模板中的正确位置。

For example:例如:

When a user runs the code it will pop up an input box and it will ask him to type an ID,当用户运行代码时,它会弹出一个输入框,并要求他输入一个 ID,
for example: 322783145例如:322783145
it will then check where the ID is placed(which row in A column) in the excel sheet which is A3 for this example and will pull the rest of this person's information into the word template I have created beforehand.(ID, Name, Last Name, Country and Date).然后它会检查 ID 在 Excel 表格中的位置(A 列中的哪一行),对于本示例为 A3,并将此人的其余信息拉入我事先创建的单词模板中。(ID、姓名、姓氏)姓名、国家和日期)。

Later on I want to be able to automatically save it to pdf as well with the same button but first I want this to work.稍后我希望能够使用相同的按钮将其自动保存为 pdf,但首先我希望它可以工作。

Thanks in advance :D提前致谢

You don't need VBA for this with the question as originally posted - simply use a SKIPIF field coded as:对于最初发布的问题,您不需要 VBA - 只需使用编码为的 SKIPIF 字段:

{SKIPIF{MERGEFIELD ID}<> {FILLIN "Which ID do you want?" \o}}

or:或者:

{SKIPIF«ID»<> {FILLIN "Which ID do you want?" \o}}

where 'ID' is the name of the data field in Excel you want to the merge to process.其中“ID”是 Excel 中要合并处理的数据字段的名称。

With that, only the record containing that ID will be processed.这样,只会处理包含该 ID 的记录。 No VBA required.不需要VBA。

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.字段结构中表示的空格都是必需的。

I see that you've added two new requirements, viz:我看到您添加了两个新要求,即:

  • the mailmerge to be run from a button in Excel;要从 Excel 中的按钮运行的邮件合并; and
  • for it "to be able to automatically save it to pdf as well".因为它“能够自动将其保存为 pdf”。

You really shouldn't move the goal posts this way.你真的不应该这样移动球门柱。 Nor should you ask ask multiple distinct questions in the same thread.您也不应该在同一线程中提出多个不同的问题。 For code to run a merge from Excel (and send the output to PDF), see Run a Mailmerge from Excel, Sending the Output to Individual Files in the Mailmerge Tips & Tricks thread at: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html .有关从 Excel 运行合并(并将输出发送到 PDF)的代码,请参阅从 Excel 运行邮件合并,将输出发送到邮件合并提示和技巧线程中的单个文件网址为: https : //www.msofficeforums.com/mail -merge/21803-mailmerge-tips-tricks.html Of course, once you get into using a macro such as this, you will need to add the logic for the ID selection to that code.当然,一旦您开始使用这样的宏,您就需要将 ID 选择的逻辑添加到该代码中。 Ask in a separate thread if you can't figure that out.如果您无法弄清楚,请在单独的线程中询问。

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

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