excel - 对象 'Add' 的方法 "workbooks"失败 - 使用 vba Access 2007 导入 Excel 工作簿

标签 excel ms-access ms-access-2007 vba

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

<小时/>

感谢您花一点时间查看我的消息。我编写了一个脚本(在很大程度上要感谢此处对 SO 的大力帮助),该脚本采用 Excel 工作簿并将每个工作表导入到 Access 2007 数据库中的单独表中。该脚本过去在我的电脑上运行良好,但自从最近从硬件故障中恢复以来,我一直无法运行该脚本。最重要的是,我的客户收到的错误消息与我自己的错误消息不同。

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

编辑:当前代码再次更新,与下面 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 行发生故障,并显示以下消息:

"Method 'Add' of object 'Workbooks' failed"

我的问题有两个: a) 我是否正确实现了后期绑定(bind)?和 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

在后期绑定(bind)中,代码将无法理解 Activesheet 是什么

跟进

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

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

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

关于excel - 对象 'Add' 的方法 "workbooks"失败 - 使用 vba Access 2007 导入 Excel 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16069312/

相关文章:

sql - ms-access:而不是行源,从查询运行

c# - 使用此查询时出现 SQL 语法错误

c# - 复制到剪贴板限制

vba - 如何将变量从一个子传递到另一个?

excel - 将值粘贴到用户选择的单元格范围,有时单元格之间为空白

ms-access - MsysObjects 值 -32758、-32757 和 3 的含义 (Microsoft Access)

ms-access - 连续形式只引用第一条记录

javascript - HTML 表格 - Excel 导出不工作 Google Chrome 和 IE

ms-access - 将文件拖放到 Microsoft Access 中

sql - 如何计算两个表的列值之间的差异