[英]Method 'Add' of object “workbooks” failed - Importing excel workbook with vba Access 2007
RESOLVED: I've accepted an answer below from Siddharth. 决议:我接受了以下Siddharth的回答。 I greatly appreciate everyone's help and I am amazed at the swift responses.
我非常感谢大家的帮助,我对迅速的反应感到惊讶。 I always learn something new when coming to this community for help, you guys are awesome.
来到这个社区寻求帮助时,我总是学到新东西,你们真棒。
Thanks for taking a moment to look at my message. 感谢您花点时间查看我的消息。 I've put together a script (thanks in no small part to the great help here on SO) that takes an excel workbook and imports each worksheet to a separate table in an Access 2007 database.
我已经整理了一个脚本(在很大程度上要归功于SO上的帮助),它采用excel工作簿并将每个工作表导入Access 2007数据库中的单独表。 The script used to work fine on my PC but since a recent recovery from a hardware failure I haven't been able to get the script to run.
该脚本曾经在我的电脑上正常工作但是由于最近从硬件故障中恢复,我无法让脚本运行。 To top it off, my client is getting different error messages than my own.
最重要的是,我的客户端收到的错误消息与我自己的不同。
A large part of the issue has to do with my object references, when I have the Microsoft Excel 14 Object Library added as a reference from the tools menu, all works fine. 该问题的很大一部分与我的对象引用有关,当我从工具菜单中添加Microsoft Excel 14对象库作为参考时,一切正常。 However, the client has a different version of Office on their systems and wishes this app to be distributed to others who may have other versions of office installed.
但是,客户端在其系统上具有不同版本的Office,并希望将此应用程序分发给可能安装了其他版本的Office的其他人。 I've tried to implement some form of late binding, but I may not be approaching this correctly.
我试图实现某种形式的后期绑定,但我可能没有正确地接近这一点。 Code is below:
代码如下:
edit: current code updated again, related to the accepted post from Siddharth below 编辑:当前代码再次更新,与下面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
The failure seems to occur for the client on the line Set excelbook = excelApp.Workbooks.Add
with this message: 使用此消息
Set excelbook = excelApp.Workbooks.Add
的行上的客户端似乎发生了故障:
My question is somewhat twofold: a) Have I implemented late binding properly? 我的问题有点双重:a)我是否正确实施了后期绑定? and b) How can I resolve this error while making sure to keep the script independent of a specific Office release?
和b)如何在确保脚本独立于特定Office版本的同时解决此错误?
Thanks for any help you can provide! 感谢您的任何帮助,您可以提供!
I believe the error is in this line 我相信错误就在这一行
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, ActiveSheet.Name, _
strFilePath, True, _
excelbook.Worksheets(intCounter).Name & "!" & _
Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "")
ActiveSheet.Name
<+++++ This is causing the error. ActiveSheet.Name
<+++++这导致错误。
Change that to excelbook.Worksheets(intCounter).Name
将其更改为
excelbook.Worksheets(intCounter).Name
In Latebinding the code will not understand what Activesheet
is 在Latebinding中,代码将无法理解
Activesheet
是什么
FOLLOWUP 跟进
You are getting a compile error because you did not add " _" at the end of the first line in DoCmd.TransferSpreadsheet
您收到编译错误,因为您没有在
DoCmd.TransferSpreadsheet
的第一行末尾添加“_”
Copy the below code and paste it as it is in your code. 复制以下代码并将其粘贴到您的代码中。
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
excelbook.Worksheets(intCounter).Name, _
strFilePath, True, _
excelbook.Worksheets(intCounter).Name & "!" & _
Replace(excelbook.Worksheets(intCounter).UsedRange.Address, "$", "")
The Workbooks.Add
method creates a new workbook. Workbooks.Add
方法创建一个新工作簿。 I don't understand why you're using that. 我不明白你为什么要用它。 Seems so me you want the user to select an existing workbook and open that, so you should use
Workbooks.Open
instead. 似乎所以我希望用户选择现有的工作簿并打开它,因此您应该使用
Workbooks.Open
。
As to whether you have implemented late binding correctly, make sure your code module includes Option Explicit
in its Declarations section (see Gord's answer for useful details), and then run Debug->Compile from the VB Editor's main menu. 至于您是否正确实现了后期绑定,请确保您的代码模块在其声明部分中包含
Option Explicit
(请参阅Gord的答案以获取有用的详细信息),然后从VB Editor的主菜单中运行Debug-> Compile。 That effort will alert you to anything in your code (objects, properties, methods, constants) for which you need further work to make them compatible with late binding. 这项工作将提醒您代码中的任何内容(对象,属性,方法,常量),您需要进一步努力使它们与后期绑定兼容。
This is a big red flag: 这是一个大红旗:
DoCmd.SetWarnings False
Turning SetWarnings
off can suppress error information which you need to help understand why your code isn't doing what you want. 关闭
SetWarnings
可以抑制您需要的错误信息,以帮助理解为什么您的代码没有按照您的意愿执行操作。 If you feel you must turn off SetWarnings
in your production code, at least leave it on during development and debugging. 如果您认为必须在生产代码中关闭
SetWarnings
,请至少在开发和调试期间将其保留。
I'll add this just for reference... In the VBA IDE Window choose Tools > Options...
and ensure that the "Require Variable Declaration" box has a check mark in it: 我将添加它仅供参考...在VBA IDE窗口中选择
Tools > Options...
并确保“需要变量声明”框中有一个复选标记:
Then, check all of your existing modules to make sure that the very first two lines are 然后,检查所有现有模块以确保前两行是
Option Compare Database
Option Explicit
The Option Explicit
statement will be added to any new Modules that you create, but you'll need to add it yourself for any existing Modules. Option Explicit
语句将添加到您创建的任何新模块中,但您需要自己为任何现有模块添加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.