方法“添加”对象“工作簿”失败 – 导入excel工作簿与VBA访问2007年

解决:我已经接受了来自悉达思的一个答案。 我非常感谢大家的帮助,我对这些迅速的回应感到惊讶。 当我来到这个社区寻求帮助时,我总是会学到新东西,你们真棒。


感谢您抽出一点时间来看看我的信息。 我已经把一个脚本放在一起(非常感谢这里的帮助),它需要一个excel工作簿,并将每个工作表导入Access 2007数据库中的一个单独的表。 该脚本用于在我的电脑上正常工作,但由于最近从硬件故障中恢复,我无法获得脚本运行。 最重要的是,我的客户得到的不是我自己的错误信息。

这个问题的很大一部分与我的对象引用有关,当我从工具菜单中添加Microsoft Excel 14对象库作为参考时,所有工作都正常。 但是,客户在他们的系统上有不同版本的Office,希望这个应用程序可以分发给其他可能安装了其他版本的Office。 我试图实现某种forms的后期绑定,但我可能没有正确接近这一点。 代码如下:

编辑:当前的代码再次更新,与从Siddharth下面接受的post有关

Private Sub Command101_Click() On Error GoTo Err_Command101_Click ' Set up excel object Dim excelApp As Object Set excelApp = CreateObject("Excel.Application") ' Set up workbook object Dim excelbook As Object ' Set up file selection objects with parameters Dim fileSelection As Object Dim intNoOfSheets As Integer, intCounter As Integer Dim strFilePath As String, strLastDataColumn As String Dim strLastDataRow As String, strLastDataCell As String ' Prompt user with file open dialog Set fileSelection = Application.FileDialog(1) fileSelection.AllowMultiSelect = False fileSelection.Show ' Get the selected file path strFilePath = fileSelection.SelectedItems.Item(1) ' Open the workbook using the file path Set excelbook = excelApp.Workbooks.Open(strFilePath) ' Get the number of worksheets intNoOfSheets = excelbook.Worksheets.Count ' Set up object for current sheet name Dim CurrSheetName As String ' Disable errors DoCmd.SetWarnings False ' Loop through each sheet, adding data to the named table that matches the sheet For intCounter = 1 To intNoOfSheets excelbook.Worksheets(intCounter).Activate DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _ excelbook.Worksheets(intCounter).Name, strFilePath, True, _ excelbook.Worksheets(intCounter).Name & "!" & _ Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "") Next ' Close Excel objects excelbook.Close excelApp.Quit Set excelApp = Nothing ' Confirmation message MsgBox "Data import Complete!", vbOKOnly, "" DoCmd.SetWarnings True Err_Command101_Click: MsgBox Err.Description End Sub 

失败似乎发生在线上的客户端Set excelbook = excelApp.Workbooks.Add与此消息:

“方法”添加“对象”工作簿“失败”

我的问题有两方面:a)我是否正确实施了后期绑定? 和b)如何解决此错误,同时确保脚本独立于特定的Office版本?

感谢您的任何帮助,您可以提供!

我相信错误在这一行

 DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, ActiveSheet.Name, _ strFilePath, True, _ excelbook.Worksheets(intCounter).Name & "!" & _ Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "") 

ActiveSheet.Name <+++++这是导致错误。

将其更改为excelbook.Worksheets(intCounter).Name

在Latebinding中,代码不会理解Activesheet是什么

跟进

您正在收到编译错误,因为您没有在DoCmd.TransferSpreadsheet的第一行的末尾添加“_”

复制下面的代码并将其粘贴到代码中。

 DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _ excelbook.Worksheets(intCounter).Name, _ strFilePath, True, _ excelbook.Worksheets(intCounter).Name & "!" & _ Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "") 

Workbooks.Add方法创build一个新的工作簿。 我不明白你为什么用这个。 看来,所以我希望用户select一个现有的工作簿,并打开它,所以你应该使用Workbooks.Open代替。

至于你是否已经正确地实现了后期绑定,请确保你的代码模块在其Declarations部分包含了Option Explicit (请参阅Gord的答案以获取有用的细节),然后从VB编辑器的主菜单运行Debug-> Compile。 这种努力会提醒你任何你的代码(对象,属性,方法,常量),你需要进一步的工作,使他们与后期绑定兼容。

这是一个大红旗:

 DoCmd.SetWarnings False 

closuresSetWarnings可以抑制错误信息,您需要帮助理解为什么您的代码没有做你想做的事情。 如果您觉得必须closures生产代码中的SetWarnings ,则至less应在开发和debugging期间保持开启状态。

我将添加此仅供参考…在VBA IDE窗口中selectTools > Options...并确保“需要variables声明”框中有一个复选标记:

vbaOptions

然后,检查所有现有的模块,确保前两行是

 Option Compare Database Option Explicit 

Option Explicit语句将被添加到您创build的任何新的模块中,但您需要为任何现有的模块自行添加它。