vba 在 Excel 中创建一个生成 Word 文档的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5224342/
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 12:46:49  来源:igfitidea点击:

Create a macro in Excel that generates a Word doc

excelexcel-vbams-wordword-vbavba

提问by jen

I would like to create a macro in Excel to copy data from specific cells in an Excel file and populate those values into a MS Word form using Office 2007. I would need it to create a new Word document in the same template for each Excel row. As new rows are added, an additional Word document is generated. It may be better if the user can specify the row.

我想在 Excel 中创建一个宏,以从 Excel 文件中的特定单元格复制数据,并使用 Office 2007 将这些值填充到 MS Word 表单中。我需要它为每个 Excel 行在同一模板中创建一个新的 Word 文档. 添加新行时,会生成一个额外的 Word 文档。如果用户可以指定行可能会更好。

Is this possible? Essentially the Excel file contains name, contact info and I'd like to populate that information into a Word document to avoid manual, duplicate entry.

这可能吗?本质上,Excel 文件包含姓名、联系信息,我想将该信息填充到 Word 文档中,以避免手动重复输入。

回答by ThomasMcLeod

If your question is whether this can be done through VBA, the answer is yes. This will require some VBA programming ability as well as knowledge of both the Word and Excel object heiracrhies. You can use Office automation to create an instance of Word:

如果您的问题是这是否可以通过 VBA 完成,那么答案是肯定的。这将需要一些 VBA 编程能力以及 Word 和 Excel 对象层次结构的知识。您可以使用 Office 自动化来创建 Word 的实例:

Dim objWordApp As Word.Application
Set objWordApp = CreateObject("Word.Application")

You can use the object to open and save documents, manipulate document content, etc.

您可以使用该对象打开和保存文档、操作文档内容等。