【发布时间】:2014-07-25 10:36:50
【问题描述】:
我正在尝试将工作簿中的所有电子表格导入 Access。但是,即使我没有收到错误消息,也没有任何内容导入 Access。一切正常,除了下面提到的那一行,尽管 Access 似乎正在导入电子表格,但我的表中没有任何内容。
Public Sub Import_Excel_Workbook()
Dim strFile As String
Dim StrFldrPath As String
Dim strfilelist() As String
Dim intFile As Integer
Dim filename As String
DoCmd.SetWarnings False
StrFldrPath = "C:\Documents\SPY\New\"
'Loop through the folder & build file list
strFile = Dir(StrFldrPath & "*.xls")
' (commented-out code removed for clarity)
Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase "C:\Documents\Database2.accdb" 'not dynamic yet
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents\SPY\New\SPY_1.xls") 'not dynamic yet
Set colWorksheets = objWorkbook.Worksheets
'cycle through the list of files
'For intFile = 1 To UBound(strfilelist)
'filename = StrFldrPath & strfilelist(intFile) (removed for the time being)
For Each objWorksheet In colWorksheets
Set objRange = objWorksheet.UsedRange
strWorksheetName = objWorksheet.Name & "!" & objRange.Address(False, False)
'########## LINE BELOW SEEMS TO FAIL ############
objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"S&P", "C:\Documents\SPY\New\SPY_1.xls", True, strWorksheetName 'not dynamic yet
Next
'Next intFile
DoCmd.SetWarnings True
End Sub
【问题讨论】:
-
摆脱所有设置警告语句。它们是一个陷阱,因为它们隐藏了您需要了解的错误。见stackoverflow.com/questions/11213892/…