Question

We are trying to access the SaveFormat property of a Microsoft.Office.Interop.Word.Document instance (I guess it's technically a DocumentClass instance since Document is an interface). Upon attempting to read SaveFormat, Word crashes rather ungracefully. Wrapping the access in a try..catch block is no good, either; control is getting passed to another assembly which decides to terminate the app rather than throwing the exception for me to re-catch.

This occurs whether I'm simply reading the property in code, like this:

WdSaveFormat saveFormat = this.document.SaveFormat; // document is a Microsoft.Office.Interop.Word.Document

Or when I access the property in a Watch using the debugger.

I suspect the document instance itself is fine; its other properties (e.g., Path) can be reflected on. Running a quickwatch on the document object yields legitimate-looking values rather than a bunch of red-flag-raising "could not evaluate", "null", etc. values. I can scroll down line-by-line in the Quickwatch window and once it gets to SaveFormat it blows up.

The crash occurs in Word 2007 as well as Word 2010.

I've tried both embedding the PIA types and not embedding them ("Embed Interop Types" flag in reference properties)

I've tried referencing the Word 2007 PIAs instead of the 2010 versions. Same behavior.

We're targeting .NET 4.0 in our projects, but I've also tried targeting 3.5. No change.

Any ideas? About to defenestrate my comp :)

Was it helpful?

Solution

It turns out the document was a zero-byte file. Our bad, but it would have been nice had Word thrown a helpful exception (similar to what happens when you try accessing Application.ActiveDocument with no document open) rather than just blow up. Oh well, grand mystery solved. :) Thanks 0xA3 for suggestion that maybe the document itself was the issue. Ever get on one track of thinking and forget to look at the obvious?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top