none
Trouble Closing VSTO Word Template Application from code behind, Crashes Word RRS feed

  • Question

  • I am new to VSTO for Microsoft Word, so please bear with me if I ask a foolish questions:-)

    I have built a little VSTO application based on a .dotx Word Template.

    The basic application is working correctly and filling the wordtemplate correctly. I am now trying to save the resulting document and close word after that.

    The code below works, but leaves Word hanging and I am not able to close it except via the TaskManager.

    What am I doing wrong?

    Your help is greatly appreciated.

    try

                {
                    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                    object saveChanges = Word.WdSaveOptions.wdSaveChanges;

                    Word._Document document = Application.ActiveDocument;
                    document.Close(ref saveChanges, System.Type.Missing, System.Type.Missing);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }


    Br.

    Wednesday, February 22, 2012 4:13 PM

Answers

All replies

  • Just to let you know.

    For your information, my setups (I tried on 2 different machines, 1 with Windows 7 Professional (64 bit) + Office 2010, and 1 with Windows 7 Ultimate (32 bit) + Word 2007. In both cases it crashes in the same manner, and with the same error.

    Br.

    Wednesday, February 22, 2012 7:03 PM
  • Hi,

    The Word application was not hanging. You closed all the documents within the Word appication but leave the Word application still running, this is exactly what was happening. If you want to close the Word application as well, you need to call Application.Quit method, see:

    http://msdn.microsoft.com/en-us/library/ff844895.aspx

    I hope this helps.


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us


    Friday, February 24, 2012 8:49 AM
    Moderator
  • The error i am getting is: System.AppDomainUnloadedException and word becomes unresponsive and cannto be close except by way of the taskmanager.
    Friday, February 24, 2012 9:13 AM
  • Ok, would you like to tell me on which line of the code VS executes causes the error message and Word hanging?


    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 24, 2012 9:20 AM
    Moderator
  • This one:

    document.Close(ref saveChanges, System.Type.Missing, System.Type.Missing);

    Br.

    Friday, February 24, 2012 10:34 AM
  • hi,

    did you ever get to the bottom of this?

    Thursday, November 6, 2014 2:58 PM
  • Yes, I think so. But cannot remember it was years ago:-)
    Thursday, November 6, 2014 3:28 PM
  • aaarrrgghhhh!

    dig it out if you can please? would seriously be helping me a LOT...

    Thursday, November 6, 2014 3:35 PM