none
My C# program generates exception when closing Word document RRS feed

  • Question

  • My C# program has two buttons, start and close. On start it opens the word application and creates a document. On close, it closes the document and exists. The issue is when the user closes the document directly (not using my program via the close button) and therefore I get exception error. There could be several other word documents active but the one was opened via my program if closed I get the error. any help is appreciated. 
    Saturday, February 15, 2014 8:10 AM

Answers

  • Hello rassuls,

    What exception do you get in the code?

    The Close method of Document class accepts three parameters. Did you try to specify all of them explicitly?

    > the one was opened via my program if closed I get the error

    It is expected that you get an exception trying to access non-existing documents. I'd recommend using the try/catch blocks in the code.

    • Marked as answer by rassuls Saturday, February 15, 2014 9:19 PM
    Saturday, February 15, 2014 7:40 PM

All replies

  • Hello rassuls,

    Did you try to debug? What line of code fires the exception?

    Saturday, February 15, 2014 8:48 AM
  • Hello rassuls,

    Did you try to debug? What line of code fires the exception?

    t happens only if already word is running with some other documents opened and my program adds another document to the existing word instance (my understanding is that there is only one instance of word running), otherwise if it is the only document and is closed by the user it is okay because my program check for existence of word process "WINWORD.EXE" and therefore I skip the document/application close.

    To answer your question when oDoc.Close() is executed the exception happens. oDoc is declared as:

    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    By the way, thanks for your help.


    Saturday, February 15, 2014 7:40 PM
  • Hello rassuls,

    What exception do you get in the code?

    The Close method of Document class accepts three parameters. Did you try to specify all of them explicitly?

    > the one was opened via my program if closed I get the error

    It is expected that you get an exception trying to access non-existing documents. I'd recommend using the try/catch blocks in the code.

    • Marked as answer by rassuls Saturday, February 15, 2014 9:19 PM
    Saturday, February 15, 2014 7:40 PM
  • Hello rassuls,

    What exception do you get in the code?

    The Close method of Document class accepts three parameters. Did you try to specify all of them explicitly?

    > the one was opened via my program if closed I get the error

    It is expected that you get an exception trying to access non-existing documents. I'd recommend using the try/catch blocks in the code.

    Thank you Eugene, using try/catch solve the problem.
    Saturday, February 15, 2014 9:21 PM