vba Access 2007 中 VB 中的“未定义用户定义类型”错误

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

“User-defined type not defined” error in VB in Access 2007

ms-accessvbams-access-2007ms-office

提问by Gedalya

I'm receiving a compile error on the following line of code:

我在以下代码行中收到编译错误:

Dim oXL As Excel.Application

The code is in VB in MS Access 2007. The line above is the beginning of a segment to generate an MS Excel file. The obvious answer to me was to ensure that the "Microsoft Office 12.0 Object Library" is checked under Tools > References. I've done this but the error persists. Does Excel need to be installed side-by-side for this to work? What have I done wrong? Thanks in advance.

代码在 MS Access 2007 中的 VB 中。上面的行是生成 MS Excel 文件的段的开头。对我来说,显而易见的答案是确保在“工具”>“引用”下检查“Microsoft Office 12.0 对象库”。我已经这样做了,但错误仍然存​​在。Excel 是否需要并排安装才能工作?我做错了什么?提前致谢。

回答by Daniel

You need to reference Microsoft Excel 12.0 Object Libraryor use late binding. Late binding is almost always necessary if you will be sharing your project with users who may have different versions of Excel installed.

您需要引用Microsoft Excel 12.0 Object Library或使用后期绑定。如果您要与可能安装了不同版本 Excel 的用户共享您的项目,则几乎总是需要后期绑定。

For late binding, you would instead do:

对于后期绑定,您可以这样做:

Dim oXL as object
Set oXL = CreateObject("Excel.Application")

Then your code should work as expected, without the need to make the reference... assuming you aren't using any other Excel specific values or objects.

然后您的代码应该按预期工作,而无需进行引用......假设您没有使用任何其他 Excel 特定值或对象。