什么是“未定义编译错误用户定义类型"?
我正在尝试将工作表从Excel 2007导出到Windows 7上的Access 2007,包括创建新表.我已经在Excel中尝试了以下VBA,但状态为未定义编译错误用户定义类型",然后突出显示Access对象的第一个变量声明.我引用了Microsoft ActiveX数据对象6.1库.
I am attempting to export a worksheet from Excel 2007 to Access 2007 on Windows 7 including creating a new table. I have tried the following VBA in Excel but it states "Compile Error User-defined types not defined" and then highlights the first variable declaration for the Access object. I have the Microsoft ActiveX Data Objects 6.1 Library referenced.
怎么了?
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
考虑使用后期绑定,如果将应用程序分发给可能安装了不同库版本类型的多个用户,则有时甚至建议使用.
Consider using late binding which is even sometimes recommended if distributing applications to multiple users who may have different library version types installed.
后期绑定避免了在CPU搜索适当类型和不支持新建"关键字:
Late binding avoids needs to specifically choose references as CPU searches for appropriate type and for applications that do not support the "New" keyword:
Dim acc As Object
Set acc = CreateObject("Access.Application")