Reputation: 117
I am using C# and Microsoft.Office.Interop.Word to work with Microsoft word document. The trouble is when I rename an image to the file extension .doc and then call the com object as follows it hangs. There must be a way to test if the file is a valid word document before attempting to open it. Anyone have an answer?
ApplicationClass.Documents.Open(ref path,
ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing);
Upvotes: 0
Views: 1664
Reputation: 1871
you can check if the if document exists with
(System.IO.File.Exists((string)fileName)) {
} and since the path is a string you can use a substring for the last 3 - 4 signs. Or you can split it with the delimiter '.' And that should give you the first and last part.
Upvotes: 1
Reputation: 117
After some testing I decided to use Aspose: http://www.aspose.com/
It turns out this is very powerful and simple to use. It throws an exception when a document is not in the correct format which can be caught and handled.
Upvotes: 1
Reputation: 24988
You could check the first handful of bytes in the file, check out the Word 97/8 file format - especially the section on the content of the FIB (file information block). .docx files will need different handling of course.
Upvotes: 0