VBA:将Excel数据读入Word

问题描述:

我正在制作一个简单的表单,用于从excel工作表中提取数据,例如姓名,出生日期和地址.并将其插入我的文字表格中,每次我都会做20-30张纸,因此我认为它可以保存复印件和复印件.粘贴时间.

I am making a simple form that extract the data from my excel sheet, such as name, date of birth, and address. And inserting them into my word form, I am doing 20-30 sheets everytime, so I think it might be able to save the copying & pasting time.

我试图遵循本教程: http://www. makeuseof.com/tag/integrate-excel-data-word-document/ 并创建了一个带有简单标签m_name的按钮,用于标记成员的姓名.但是它告诉我Compile error: User-defined type not defined.并在第1行上标记.

I tried to follow this tutorial: http://www.makeuseof.com/tag/integrate-excel-data-word-document/ And created a button with a simple label named m_name, for member's name. But it tells me Compile error: User-defined type not defined. And flaged on line 1.

我正在使用Word 2003,(按照指南的要求,我无法找到工具">参考").我不确定是否与此错误有关.

I am using Word 2003, (I am not able to find the Tools > Reference as the guide was asking for). I am not sure if it is related to this error.

Private Sub CommandButton1_Click()
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook

Set exWb = objExcel.Workbooks.Open("U:\test.xls")
ThisDocument.m_name.Caption = exWb.Sheets("Member's Data").Cells(3, 3)

exWb.Close

Set exWb = Nothing

End Sub

是的,根据教程设置引用非常重要.

Yes, it's very important to set references according to the tutorial.

但是,更改这两行:

Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook

收件人:

Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")

并且代码也应该工作.