已解决:我已经接受了 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 版本的同时解决此错误?
感谢您的任何帮助,您可以提供!