什么是 "Compile error User-defined type not defined"?

What is "Compile error User-defined type not defined"?

我试图在 Windows 7 上将工作表从 Excel 2007 导出到 Access 2007,包括创建一个新的 table。我在 Excel 中尝试了以下 VBA 但它声明 "Compile Error User-defined types not defined" 然后突出显示 Access 对象的第一个变量声明。我引用了 Microsoft ActiveX 数据对象 6.1 库。

怎么了?

Dim acc As New Access.Application
Dim ws As Worksheet
Dim ThisWorkbook As Workbook
Dim a As Long
Dim b As Long

Set ThisWorkbook = ActiveWorkbook
Set ws = ThisWorkbook.Sheets("CompleteNoFormat3")
a = ws.UsedRange.Rows.Count
b = ws.UsedRange.Columns.Count

acc.OpenCurrentDatabase "CustDB.mdb"
  acc.DoCmd.TransferSpreadsheet _
   TransferType:=acImport, _
   SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
   TableName:="CustExportTable", _
   Filename:=Application.ActiveWorkbook.FullName, _
   HasFieldNames:=True, _
   Range:="BAPCompletNoFormat$A1:" & a & B 
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing

考虑使用后期绑定,如果将应用程序分发给可能安装了不同库版本类型的多个用户,有时甚至会推荐使用后期绑定。

后期绑定避免需要专门选择引用,因为 CPU 搜索合适的类型和不适合的应用程序 support the "New" keyword:

Dim acc As Object

Set acc = CreateObject("Access.Application")