繁体   English   中英

方法“添加”对象“工作簿”失败 - 使用vba Access 2007导入excel工作簿

[英]Method 'Add' of object “workbooks” failed - Importing excel workbook with vba Access 2007

决议:我接受了以下Siddharth的回答。 我非常感谢大家的帮助,我对迅速的反应感到惊讶。 来到这个社区寻求帮助时,我总是学到新东西,你们真棒。


感谢您花点时间查看我的消息。 我已经整理了一个脚本(在很大程度上要归功于SO上的帮助),它采用excel工作簿并将每个工作表导入Access 2007数据库中的单独表。 该脚本曾经在我的电脑上正常工作但是由于最近从硬件故障中恢复,我无法让脚本运行。 最重要的是,我的客户端收到的错误消息与我自己的不同。

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

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

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方法创建一个新工作簿。 我不明白你为什么要用它。 似乎所以我希望用户选择现有的工作簿并打开它,因此您应该使用Workbooks.Open

至于您是否正确实现了后期绑定,请确保您的代码模块在其声明部分中包含Option Explicit (请参阅Gord的答案以获取有用的详细信息),然后从VB Editor的主菜单中运行Debug-> Compile。 这项工作将提醒您代码中的任何内容(对象,属性,方法,常量),您需要进一步努力使它们与后期绑定兼容。

这是一个大红旗:

DoCmd.SetWarnings False

关闭SetWarnings可以抑制您需要的错误信息,以帮助理解为什么您的代码没有按照您的意愿执行操作。 如果您认为必须在生产代码中关闭SetWarnings ,请至少在开发和调试期间将其保留。

我将添加它仅供参考...在VBA IDE窗口中选择Tools > Options...并确保“需要变量声明”框中有一个复选标记:

vbaOptions

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

Option Compare Database
Option Explicit

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM