我有一个代码可以帮助我单击一下按钮导入Excel电子表格。该代码有效,我可以导入大多数Excel电子表格。但是,有些Excel电子表格无法导入,我也不知道为什么。
Option Compare Database
Private Sub btnBrowse_Click()
Dim diag As Office.FileDialog
Dim item As Variant
Set diag = Application.FileDialog(msoFileDialogFilePicker)
diag.AllowMultiSelect = False
diag.Title = "Please select an Excel Spreadsheet"
diag.Filters.Clear
diag.Filters.Add "Excel Spreadsheet", "*.xls, *.xlsx"
If diag.Show Then
For Each item In diag.SelectedItems
Me.txtFileName = item
Next
End If
End Sub
Private Sub btnHome_Click()
DoCmd.OpenForm ("MainFrm")
End Sub
Private Sub btnImport_Click()
Dim FSO As New FileSystemObject
If Nz(Me.txtFileName, "") = "" Then
MsgBox "Please select a file"
Exit Sub
End If
If FSO.FileExists(Nz(Me.txtFileName, "")) Then
ImportExcel.ImportExcelSpreadsheet Me.txtFileName, FSO.GetFileName(Me.txtFileName)
Else
MsgBox "File not found"
End If
End Sub
Public Sub ImportExcelSpreadsheet(fileName As String, tableName As String)
On Error GoTo BadFormat
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tableName, fileName, True
Exit Sub
BadFormat:
MsgBox "The file you tried to import was not an Excel Spreadsheet"
End Sub
一些我无法导入的Excel电子表格文件将显示我在代码中键入的消息。文件类型绝对是Excel电子表格,但是我不知如何无法导入。我想知道是否是因为我的代码。
答案 0 :(得分:0)
SpreadsheetType 应该为acSpreadsheetTypeExcel12Xml
,至少是第一次尝试。