[英]excel Method 'open' of object 'workbooks' failed error when opening a read only workbook
[英]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...
並確保“需要變量聲明”框中有一個復選標記:
然后,檢查所有現有模塊以確保前兩行是
Option Compare Database
Option Explicit
Option Explicit
語句將添加到您創建的任何新模塊中,但您需要自己為任何現有模塊添加它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.