none
Word VSTO 2010 - Word crashes after Document.Close RRS feed

  • Question

  • Good day

    I have created a Word 2010 addin using C# (VS2010). The premis of the system
    is that a user is able to download a template from a website which contains
    content controls (.docx document). The addin then retrieves data from a database
    through a web service to populate these controls.

    The user would create a list of documents to be processed and populate the
    template. When more than one document needs to be created, the addin saves the
    template file to the user's My Documents folder, and opens this "temporary" file
    for each document to be created. Once all processing is complete, I save and then close the
    document using Document.Close() and open the temp file again to process the next
    document. Once all "processing" is complete, I merge the created documents into
    one and then word crashes.

    Word does not crash if I do not close the documents. It seems that word does
    not release the closed documents from memory. Has anyone had any similar
    issues?

    I have tried to release the object by marshaling, also tried closing the
    document with the following to no avail:

    [DllImport("user32.dll", SetLastError =
    true, CharSet = CharSet.Auto)]

    privatestaticexternIntPtrFindWindow(stringlpClassName, stringlpWindowName);

    [DllImport("user32.dll", SetLastError =
    true, CharSet = CharSet.Auto)]

    privatestaticexternboolPostMessage(IntPtrhWnd, uintmsg, IntPtrwParam, IntPtrlParam);

    const intwmClose = 16;

    var hWnd = FindWindow(null, this.Application.ActiveWindow.Caption + " - Microsoft Word");

    Fault bucket , type 0

    Event Name: APPCRASH

    Response: Not available

    Cab Id: 0 ---------------------------------------------------------------------------

    Faulting application name: WINWORD.EXE, version: 14.0.4762.1000, time stamp: 0x4bae25b7

    Faulting module name: wwlib.dll, version: 14.0.4762.1000, time stamp: 0x4bae2623

    Exception code: 0xc0000005

    Fault offset: 0x0002530e

    Faulting process id: 0xd70

    Faulting application start time: 0x01cd770bf88895ac

    Faulting application path: C:\Program Files\Microsoft Office\Office14\WINWORD.EXE

    Faulting module path: C:\Program Files\Microsoft Office\Office14\wwlib.dll

    Report Id: 57c0f2dc-e2ff-11e1-afc1-a4badbfd4226

    Monday, August 13, 2012 7:56 AM

Answers

  • Hi Tom

    Pretty much.

    I have found a solution though. I make an asynchronous method call to start processing within the Application_ProtectedViewWindowBeforeClose method and return after the async call. This ends the second WINWORD.EXE process and Word does not crash.

    private delegatevoidOutProtected(boolprocessing);

    private OutProtectedoutProtected;

    void Application_ProtectedViewWindowBeforeClose(Word.ProtectedViewWindow PvWindow, int CloseReason, ref bool Cancel)

            {

                try

                {

                    Word.Application oWord = this.Application;

                    if (oWord.ActiveProtectedViewWindow == null)

                    {

                        if (oWord.ActiveDocument != null)

                        {

                            outProtected = new OutProtected(StartProcessing);

                            outProtected.BeginInvoke(true, null, null);

                            return;  

                        }

                    }

                }

                catch (System.Runtime.InteropServices.COMException ex2)

                {

                    MessageBox.Show(ex2.ToString());

                    return;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.ToString());

                    return;

                }

                finally

                {

                   

                }

            }

    void StartProcessing(bool processing)

            {

                Word.Application oWord = this.Application;

                if (oWord.ActiveProtectedViewWindow == null)

                {

                    if (oWord.ActiveDocument != null)

                    {

                        if (!IsProcessing)

                        {

                            Word._Document Doc = oWord.ActiveDocument;

                            ProcessDoc(Doc);

                        }

                    }

                }

            }

    • Marked as answer by G_Money86 Tuesday, August 14, 2012 8:34 AM
    Tuesday, August 14, 2012 8:34 AM

All replies

  • I have also updated the Microsoft Visual Studio 2010 Tools for Office Runtime to the latest version (10.0.31119) which did not resolve the problem.

    Another thing to note is that because of our intranet security policy, the template opens in protected view when it is opened from our website. Word only crashes when closing the documents if processing was started in protected view (after user has clicked on Enable Editing). Also, word does not always crash. Sometimes, a user is able to process the same documents a couple of times without any issues. Word crashes at random but only after all processing has been completed. The merged document can be retrieved.

    void Application_ProtectedViewWindowBeforeClose(Word.ProtectedViewWindow PvWindow, int CloseReason, ref bool Cancel)

            {

                try

                {

                    if (this.Application.ActiveProtectedViewWindow == null)

                    {

                        if (this.Application.ActiveDocument != null)

                        {

                            ProcessDoc(this.Application.ActiveDocument);

                        }

                    }

                }

                catch (System.Runtime.InteropServices.COMException ex2)

                {

                    MessageBox.Show(ex2.ToString());

                    return;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.ToString());

                    return;

                }

                finally

                {

                   

                }

            }

            void Application_DocumentOpen(Word._Document Doc)

            {

                try

                {

                    if (this.Application.ActiveProtectedViewWindow == null)

                    {

                        if (this.Application.ActiveDocument != null)

                        {

                            ProcessDoc(this.Application.ActiveDocument);

                        }

                    }

                }

                catch (System.Runtime.InteropServices.COMException ex2)

                {

                    MessageBox.Show(ex2.ToString());

                    return;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.ToString());

                    return;

                }

                finally

                {

                   

                }

            }


    • Edited by G_Money86 Monday, August 13, 2012 8:17 AM
    Monday, August 13, 2012 8:01 AM
  • Hi G_Money86,

    Thanks for posting in the MSDN Forum.

    It's seems OK. I'm wondering what your will do in the ProcessDoc. And it will help me do further troubleshooting if you can provide me the exception details.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, August 14, 2012 4:40 AM
    Moderator
  • Hi Tom

    ProcessDoc(Word._Document doc) takes a word document as its only parameter (this is the template which the user downloads). We pass through a few settings in the filename of the document. This document is first saved to the user's pc and then based on the settings from the filename, a list of "documents" which are to be created are retrieved from the database. Then for each of these "documents", we open the saved template, populate the content controls from the db, remove the content controls, save the document and add its filename to a list of strings to be used to merge the docs. Once this has been done, the document is closed and the next one is opened until all document in the first list have been "processed". Then using our new list of filesnames, each one is inserted into a new file (merging). Then the processing is complete.

    Word crashes with one of those Dr Watson type messages. This does not always happen but only once all processsing has finished. The two things that cause Word to crash are if we close the processed documents AND if the template that was downloaded is first opened in Protected View. If we open the same template from a user's local file system, it does not open in protected view and is processed without any issues. We need the current functionality as it is (open directly from our website and start processing once Enable Editing is clicked).

    Edit: The addin first runs in the Application_ProtectedViewWindowBeforeClose method, then while executing in this method, Application_DocumentOpen is executed for everytime we reopen the template for a new document to be processed. Once all documents have been processed, Application_ProtectedViewWindowBeforeClose is completed.

    When the doc is opened in PV(Protected View), there are two WINWORD.EXE process that run, even though PV is closed. This seems to be the cause, either one of the COM objects not being released or the fact that the addin is sharing something between two APPDOMAINs. I haven't been able to close the PVs process using a handler (not able to get it in code). Is it possible to close the PV process or is there another work around?

    Regards

    G


    • Edited by G_Money86 Tuesday, August 14, 2012 7:01 AM
    Tuesday, August 14, 2012 6:57 AM
  • Hi G_Money86,

    So, you mean that the crash only happen when there have several WinWord threads?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, August 14, 2012 7:24 AM
    Moderator
  • Hi Tom

    Pretty much.

    I have found a solution though. I make an asynchronous method call to start processing within the Application_ProtectedViewWindowBeforeClose method and return after the async call. This ends the second WINWORD.EXE process and Word does not crash.

    private delegatevoidOutProtected(boolprocessing);

    private OutProtectedoutProtected;

    void Application_ProtectedViewWindowBeforeClose(Word.ProtectedViewWindow PvWindow, int CloseReason, ref bool Cancel)

            {

                try

                {

                    Word.Application oWord = this.Application;

                    if (oWord.ActiveProtectedViewWindow == null)

                    {

                        if (oWord.ActiveDocument != null)

                        {

                            outProtected = new OutProtected(StartProcessing);

                            outProtected.BeginInvoke(true, null, null);

                            return;  

                        }

                    }

                }

                catch (System.Runtime.InteropServices.COMException ex2)

                {

                    MessageBox.Show(ex2.ToString());

                    return;

                }

                catch (Exception ex)

                {

                    MessageBox.Show(ex.ToString());

                    return;

                }

                finally

                {

                   

                }

            }

    void StartProcessing(bool processing)

            {

                Word.Application oWord = this.Application;

                if (oWord.ActiveProtectedViewWindow == null)

                {

                    if (oWord.ActiveDocument != null)

                    {

                        if (!IsProcessing)

                        {

                            Word._Document Doc = oWord.ActiveDocument;

                            ProcessDoc(Doc);

                        }

                    }

                }

            }

    • Marked as answer by G_Money86 Tuesday, August 14, 2012 8:34 AM
    Tuesday, August 14, 2012 8:34 AM