I never had a problem when the Excel file cannot be directly determined by the extension, but if I had to do this, the first thing that comes to mind is the UNIX file utility, which identifies the type of file by looking at its contents. It recognizes a very large number of file types.
I use Cygwin for Windows, which is essentially a UNIX environment on Windows.
When I use the file command in Cygwin in an Excel 2010 file (xlsx), I renamed ".csv", I get:
$ file afile.csv afile.csv: Microsoft Excel 2007+
This is a little awkward solution, but in VBA you can develop the C:\cygwin\bin\file.exe using the Windows Script Host and capture the output for each file.
If you encode the path to an Excel file with single ticks around it (for example, "C: \ path \ to \ file"), Cygwin should interpret it correctly (Cygwin utilities expect to see a unix-like path: / path / to / file). I just checked this on a regular windows command prompt and it worked:
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 file binary in the GnuWin32 SourceForge Project , but it seems to be a bit outdated; I have not tried it, but it can still recognize current versions of Excel.
If you need your own solution for Excel, Iām not entirely sure of my head; hope someone else has done this before.
source share