VBA tool to determine SpreadsheetType from given spreadsheet

自古美人都是妖i 提交于 2019-12-24 13:42:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!