问题
I wish to use the TransferSpreadsheet command to link or import a spreadsheet to Access 2010.
It requires a value for SpreadsheetType (drawn from the AcSpreadSheetType enumeration).
What is the Access VBA tool to determine SpreadsheetType from given spreadsheet filename?
Maybe the only means is to examine the file extension and do a lookup?
If you've solved this problem before, please share.
回答1:
Given that there seems to be a definite one-to-many mapping from file extension to specific Excel formats (see table below), I decided to keep it (very) simple, and assume only a few possible formats are likely.
My code:
Private Function excel_type(file As String) As Integer
Dim ext As String
ext = LCase(extension(file))
excel_type = IIf(ext = "xlsx", acSpreadsheetTypeExcel12, _
IIf(ext = "xls", acSpreadsheetTypeExcel9, _
IIf(ext = "xml", acSpreadsheetTypeExcel12Xml, -1)))
End Function
Public Function extension(file As String) As String
extension = Right(file, Len(file) - InStrRev(file, "."))
End Function
Reference table (feel free to edit for completeness):
Extn SpreadsheetType Value File format
? acSpreadsheetTypeExcel3 0 Microsoft Excel 3.0
? acSpreadsheetTypeExcel4 6 Microsoft Excel 4.0
xls acSpreadsheetTypeExcel5 5 Microsoft Excel 5.0
xls acSpreadsheetTypeExcel7 5 Microsoft Excel 95
xls acSpreadsheetTypeExcel8 8 Microsoft Excel 97
xls acSpreadsheetTypeExcel9 8 Microsoft Excel 2000
xlsx acSpreadsheetTypeExcel12 9 Microsoft Excel 2010
xml acSpreadsheetTypeExcel12Xml 10 Microsoft Excel 2010 XML
? acSpreadsheetTypeLotusWJ2 4
? acSpreadsheetTypeLotusWJ1
? acSpreadsheetTypeLotusWJ3
? acSpreadsheetTypeLotusWJ4
来源:https://stackoverflow.com/questions/30385705/vba-tool-to-determine-spreadsheettype-from-given-spreadsheet