访问VBA如何确定文件是否为Excel格式?

使用MS Access VBA如何检查文件以了解它是否为Excel格式?

我从来没有遇到过一个Excel文件不能直接由扩展名决定的问题,但是如果我必须这样做,首先想到的就是UNIX实用程序file ,它通过查看文件types来识别文件types,内容。 它识别大量的文件types。

我使用Cygwin for Windows,这基本上是Windows上的UNIX环境。

当我在Excel 2010(xlsx)文件中使用Cygwin中的file命令时,我已将其重命名为“.csv”,我得到:

 $ file afile.csv afile.csv: Microsoft Excel 2007+ 

这是一个稍微尴尬的解决scheme,但在VBA中,您可以使用Windows Script Host C:\cygwin\bin\file.exe进程,并捕获每个文件的输出。

如果你编码的path是单个的周围的Excel文件(即“C:\path\到\文件”),Cygwin应该正确解释它(Cygwin实用程序希望看到一个类似Unix的path:/ path / to /文件)。 我刚刚在一个正常的Windows命令提示符validation了这一点,它的工作原理:

 c:\>c:\cygwin\bin\file.exe 'C:\path\to\afile.csv' C:\path\to\afile.csv: Microsoft Excel 2007+ 

GnuWin32 SourceForge项目中也有一个本地Windows文件二进制 file ,但似乎有些过时; 我还没有尝试过,但它仍然可以识别现代的Excel版本。

如果你需要一个本地的Excel解决scheme – 我不完全确定我的头顶; 希望别人之前做过这个。

这不是为Access,而是为Excel我使用这个。 这不是最好的,也不是最好的解决scheme,但要自己动手。

 Public Function IsExcelFormat(ByVal filePath As String) As Boolean On Error GoTo Nope Application.ScreenUpdating = False Dim wb As Workbook Set wb = Workbooks.Open(filePath ) IsExcelFormat = (wb.FileFormat > 50) CleanExit: Application.ScreenUpdating = True Exit Function Nope: ' Clearly not Excel format Err.clear IsExcelFormat = False Resume CleanExit: End Function 

是的,它使用Excel的automagic。 我知道。 这是可怕的。 屏幕更新不起作用。 您的任务栏将在打开closures文件时进行更新。 但是,它仍然有效。

您可能需要在Access VBA脚本中创build一个实例Excel,并可以select将其传递给像这样的函数。 注意我没有testing过这个。

 Public Function IsExcelFormat(ByVal file_path As String, _ Optional byRef excel_instance as Excel.Application = Nothing) As Boolean On Error GoTo Nope Dim local_excel as boolean If excel_instance Is Nothing Then Set excel_instance = New Excel.Application local_excel = True End If Dim wb As Excel.Workbook excel_instance.ScreenUpdating = False Set wb = excel_instance.Workbooks.Open(file_path) IsExcelFormat = (wb.FileFormat > 50) wb.Close savechanges:=False CleanExit: If local_excel Then excel_instance.Quit Else excel_instance.ScreenUpdating = True End If Exit Function Nope: ' Clearly not Excel format Err.clear IsExcelFormat = False Resume CleanExit: End Function 

关于使用ADOX的可能方法的一些注意事项

 Sub SortFiles() ''Library reference: Windows Script Host Object Model Dim fs As New FileSystemObject Dim ts As TextStream Dim sType As String Dim sFile As File For Each sFile In fs.GetFolder("Z:\Docs\").Files sType = sFile.Type If InStr(sType, "Microsoft") = 0 Then sList = ListTables(sFile.Name) If sList = "Error: Not Excel" Then ''Move to suitable folder Else Debug.Print sList Stop ''This can be read as Excel, most likely End If ElseIf sType Like "*Excel*" Then ''Includes CSV sFile.Move "z:\docs\Excelfiles\" Else sFile.Move "z:\docs\OtherMS\" End If Next End Sub Function ListTables(sFile As String) As String ''Library reference: Microsoft ADO Ext. xx for DDL and Security Dim cat As New ADOX.Catalog Dim scn As String Dim t As ADOX.Table Dim cn As New ADODB.Connection Dim sList As String On Error GoTo Handle_Err: scn = "Provider=Microsoft.ACE.OLEDB.12.0;" _ & "Data Source=" & sFile & ";Extended Properties=""Excel 8.0;HDR=No""" cn.Open scn cat.ActiveConnection = cn For Each t In cat.Tables sList = sList & vbCrLf & t.Name Next t ListTables = sList Exit_Proc: Set cn = Nothing Set cat = Nothing Exit Function Handle_Err: If Err.Number = -2147467259 Then ''External table is not in the expected format. ListTables = "Error: Not Excel" Err.Clear Resume Exit_Proc Else Debug.Print Err.Number, Err.Description End If End Function