Is there a way to programmatically tell that a Microsoft Office file is in the Open XML format and not the old pre-Office-97 binary format?
As an end user, I usually change the file extension to .zip and try extracting the archive. If it does extract, then I know that it is an Open XML format. But I am not even sure if that is a fool-proof method and that the files that it does not work for are not Open Xml files.
I did this in Powershell and from the output, because of the presence of the CustomXmlParts
property, I am able to somewhat make a guess that this is an Open XML format workbook I am looking at.
PS C:\Users\Sathyaish> $excel = New-Object -ComObject Excel.Application
PS C:\Users\Sathyaish> $workbook = $excel.Workbooks.Open("C:\Sathyaish\temp\Foo.xlsx")
PS C:\Users\Sathyaish> $workbook | select C*, F*
Creator : 1480803660
ChangeHistoryDuration : 0
Charts : System.__ComObject
CodeName :
CommandBars :
Comments :
ConflictResolution : 1
Container :
CreateBackup : False
CustomDocumentProperties : System.__ComObject
CustomViews : System.__ComObject
CalculationVersion : 171027
ContentTypeProperties :
Connections : System.__ComObject
CheckCompatibility : False
CustomXMLParts : System.__ComObject
ConnectionsDisabled : False
CaseSensitive : False
ChartDataPointTrack : True
FileFormat : 51
FullName : C:\Sathyaish\temp\Foo.xlsx
FullNameURLEncoded : C:\Sathyaish\temp\Foo.xlsx
Final : False
ForceFullCalculation : False
Is there a VSTO/VBA way to tell the same thing? Perhaps by looking at the value of the FileFormat
property as listed above? What are the valid integer values and their meanings?
Or there must be the option of trying to load the document using the Open XML SDK and if it fails, then you know that it isn't a proper Open XML file format. But that doesn't exclude other possibilities such as the file not being a Microsoft Office file at all.