Closing word from a web browser

I opened a word document from a web browser control on my windows form (c# windows application) and have access to the document object. I tried to close word without saving the document but I kept getting this error message below

**An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mycomponent.dll
**Additional information: This method or property is not available because this document is in another application.

Here is my code in a command button.

    object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    object originalFormat = Type.Missing;
    object routeDocument = Type.Missing;
  _doc.Close( ref saveChanges, ref originalFormat, ref routeDocument);

How can I handle this? I do not want to see the prompt which tries to save my document whenever I move to another document. I use a combo box to select different documents(templates)


Who is Participating?
Daniel Van Der WerkenConnect With a Mentor Independent ConsultantCommented:
Check out this URL:

You may need to call it via the standard Interop methodology by setting your parameters before hand and then calling it with InvokeMember().

something like:
<<< I may have the syntax slightly off here.  There are lots of examples on the web though >>>
object [] myParams = new object[];
myParams[0] = saveChanges;
myParams[1] = Type.Missing;
myParams[2] = Type.Missing;


_doc.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, _doc, myParams);

or maybe (without setting up the parameter object as above)

_doc.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, _doc, new object [] {saveChanges, Type.Missing, Type.Missing});

See what that does for you.

SirdotsAuthor Commented:
when I tried this

object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
_doc.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, null, _doc, new object [] {saveChanges, Type.Missing, Type.Missing});

I got this error message

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.

It is still not working.
All Courses

From novice to tech pro — start learning today.