3

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


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

问题的很大一部分与我的对象引用有关,当我从工具菜单添加 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 版本的同时解决此错误?

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

4

3 回答 3

4

我相信错误在这一行

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

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

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

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

跟进

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

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

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
excelbook.Worksheets(intCounter).Name, _
strFilePath, True, _
excelbook.Worksheets(intCounter).Name & "!" & _
Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "")
于 2013-04-17T20:26:04.593 回答
3

Workbooks.Add方法创建一个新工作簿。我不明白你为什么要使用它。似乎我希望用户选择一个现有的工作簿并打开它,所以你应该使用它Workbooks.Open

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

这是一个很大的危险信号:

DoCmd.SetWarnings False

关闭SetWarnings可以抑制错误信息,您需要这些信息来帮助理解为什么您的代码没有按照您的意愿执行。如果您觉得必须在生产代码中关闭SetWarnings,至少在开发和调试期间将其保持打开状态。

于 2013-04-17T20:18:53.203 回答
2

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

vba 选项

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

Option Compare Database
Option Explicit

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

于 2013-04-17T20:35:20.987 回答