VBA:将excel数据读入word
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16992492/
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
VBA: Reading excel data into word
提问by George
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.
我正在制作一个简单的表格,从我的 excel 表中提取数据,例如姓名、出生日期和地址。并将它们插入到我的word表格中,我每次都做20-30张,所以我认为它可能能够节省复制和粘贴的时间。
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.
我尝试按照本教程进行操作:http: //www.makeuseof.com/tag/integrate-excel-data-word-document/并创建了一个带有简单标签的按钮,名为m_name
,用于成员姓名。但它告诉我Compile error: User-defined type not defined
。并在第 1 行标记。
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.
我正在使用 Word 2003,(我无法找到指南要求的工具 > 参考)。我不确定它是否与此错误有关。
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
回答by Kazimierz Jawor
Yes, it's very important to set references according to the tutorial.
是的,根据教程设置引用非常重要。
However, change these two lines:
但是,更改这两行:
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
to:
到:
Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")
and the code should work, too.
并且代码也应该工作。