Closing word from a web browser

Posted on 2005-05-03
Last Modified: 2008-02-01
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)


Question by:Sirdots
    LVL 19

    Accepted Solution

    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.


    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now