vba 访问VBA如何确定文件是否为Excel格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13446873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access VBA how to deterine if file is Excel Format?
提问by Alex
Using MS Access VBA how can I check a file to know whether it is in Excel format?
使用 MS Access VBA 如何检查文件以了解它是否为 Excel 格式?
回答by transistor1
I have never had an issue where an Excel file can't directly be determined by extension, but if I had to do that, the first thing that comes to mind is the UNIX utility file
, which identifies a file type by looking at its' contents. It recognizes a very large number of file types.
我从来没有遇到过不能直接通过扩展名确定 Excel 文件的问题,但如果我必须这样做,首先想到的是 UNIX 实用程序file
,它通过查看其内容来识别文件类型. 它可以识别大量的文件类型。
I use Cygwinfor Windows, which is essentially a UNIX environment on Windows.
我在 Windows 上使用Cygwin,它本质上是 Windows 上的 UNIX 环境。
When I use the file
command in Cygwin on an Excel 2010 (xlsx) file I've renamed '.csv', I get:
当我file
在已重命名为“.csv”的 Excel 2010 (xlsx) 文件上使用Cygwin 中的命令时,我得到:
$ file afile.csv
afile.csv: Microsoft Excel 2007+
It's a slightly awkward solution, but in your VBA you could fork a C:\cygwin\bin\file.exe
process using Windows Script Host, and capture the output for each file.
这是一个有点尴尬的解决方案,但在您的 VBA 中,您可以C:\cygwin\bin\file.exe
使用Windows Script Host派生一个进程,并捕获每个文件的输出。
If you code the path to the Excel file with single ticks around it (i.e. 'C:\path\to\file'), Cygwin should interpret it correctly (Cygwin utilities expect to see a unix-like path: /path/to/file). I just verified this in a normal Windows command prompt, and it worked:
如果您对 Excel 文件的路径进行编码(即“C:\path\to\file”),Cygwin 应该正确解释它(Cygwin 实用程序希望看到类似 unix 的路径:/path/to/文件)。我刚刚在普通的 Windows 命令提示符中验证了这一点,并且它有效:
c:\>c:\cygwin\bin\file.exe 'C:\path\to\afile.csv'
C:\path\to\afile.csv: Microsoft Excel 2007+
There is also a native Windows binaryof file
in the GnuWin32 SourceForge project, but it seems to be a little outdated; I haven't tried it, but it may still recognize modern Excel versions.
还有一个原生的Windows二进制的file
在的GnuWin32 SourceForge项目,但它似乎有点过时; 我还没有尝试过,但它可能仍然可以识别现代 Excel 版本。
If you need a native Excel solution -- I'm not entirely sure off the top of my head; hopefully someone else has done this before.
如果您需要本机 Excel 解决方案——我并不完全确定;希望其他人以前做过这件事。
回答by cheezsteak
This isn't for Access but for Excel I use this. This is not the greatest nor anybodies preferred solution, but brace yourself.
这不是用于 Access,而是用于 Excel,我使用它。这不是最好的,也不是任何人的首选解决方案,但要振作起来。
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
Yeah it uses Excel's automagic. I know. It's hideous. And the ScreenUpdating doesn't work entirely. Your taskbar will update as you open an close the file. But still, it works.
是的,它使用 Excel 的 automagic。我知道。太可怕了 并且 ScreenUpdating 不能完全工作。当您打开和关闭文件时,您的任务栏将更新。但是,它仍然有效。
You might need to create an instance Excel in your Access VBA script and optionally pass it to function something like this. Note I haven't tested this.
您可能需要在 Access VBA 脚本中创建一个 Excel 实例,并可选择将其传递给类似这样的功能。注意我没有测试过这个。
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
回答by Fionnuala
Some notes on a possible approach using ADOX
关于使用 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. x.x 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