Error closing files and quitting Word 2007 SP1

H

Haydn Robinson

I'm using C# for Word interop and can't find another newsgroup so here goes.

I'm creating an instance of Word 2007 SP1 and successfully loading in a
file. However, when I ask it to close all documents, it fails with a COM
error of "Command Failed", HRESULT -2146824090.

The code I'm using is as follows:

// To open the document...
// Not included all variables, but should be obvious what they are...
object format = WdOpenFormat.wdOpenFormatAuto;
this.wordApplication.Documents.Open(
ref theFilename,
ref missing,
ref isReadOnly,
ref oFalse,
ref missing,
ref missing,
ref missing,
ref missing,
ref missing,
ref format,
ref missing,
ref isVisible,
ref missing,
ref missing,
ref missing,
ref missing);

And to close all documents...

object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
object missing = System.Type.Missing;
this.wordApplication.Documents.Close(ref saveChanges, ref missing, ref
missing);

When I open another document I get another window for Word which I don't
want (all documents must open in the same window).

Any suggestions welcome.

Thanks.
Haydn
 
H

Haydn Robinson

Found the problem now.

I have a Windows application that manages Word using SetParent(). When I
call SetParent() on Word and then attempt to close any documents, it fails.
The workaround is to call SetParent() again, setting its parent to null (i.e.
the desktop), call to close the documents then call SetParent() again to
remanage it.

Nice one Microsoft - never had to do this in Word 2003.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top