none
Word crashing when closing a document RRS feed

  • Question

  • My (watered down) code (below) is simple enough. But when I run it, Word crashes with an error:

    "Microsoft Office has stopped working. Windows is collecting more information about this problem. This might take several minutes"

    The event log says:

    Faulting application name: WINWORD.EXE, version: 15.0.4659.1000, time stamp: 0x5417f0a4

    Faulting module name: wwlib.dll, version: 15.0.4659.1000, time stamp: 0x5417f0e5

    Exception code: 0xc0000005

    Fault offset: 0x00000000002e7e11

    Faulting process ID: 0x51c4

    Faulting application start time: 0x01cfe951f894d7e8

    Faulting application path: C:\Program Files (x86)\Microsoft Office 2013\Office15\WINWORD.EXE

    Faulting module path: C:\Program Files (x86)\Microsoft Office 2013\Office15\wwlib.dll

    Report ID: 38531b7c-5545-11e4-be98-bc305bac4f36

    Faulting package full name:

    Faulting package-relative application ID:

    CODE
    -------

    void Application_DocumentBeforeSave(Word.Document wordDoc, ref bool saveAsUI, ref bool cancel)
    {
     wordDoc.Saved = true;
     cancel = true;
     DoUpdate(null);
    }

    public void DoUpdate(IRibbonControl control)
    {
    CloseWordDoc();
    }

    private void CloseWordDoc()
    {
        object saveChanges = false;
        object o = null;
        ((Word._Document)wordApp.ActiveDocument).Close(ref saveChanges, ref o, ref o);
    }

    Does anyone have any ideas please?

    Thanks.

    Thursday, October 16, 2014 3:03 PM

All replies

  • Hello,

    Did you try to debug the code? What line of code causes the issue?

    Anyway, I don't think that the DocumentBeforeSave event is a suitable place for closing the document.

    Thursday, October 16, 2014 3:23 PM
  • Hi, thanks for your reply.

    I did debug the code - its this line:

    ((Word._Document)wordApp.ActiveDocument).Close(ref saveChanges, ref o, ref o);

    Basically, when the user clicks Save im trying to perform-some-action and then close the document.

    Sounds like it should be do-able and straightforward even to a newbie like me...

    If DocumentBeforeSave isn't the right place to close the doc then after ive performed my action where and how can I close the doc please?

    Thursday, October 16, 2014 3:41 PM
  • Hi,

    Which version of Word are you using? I agree with Eugene that the document_beforeSave is not suitable to close the document.

    >>Basically, when the user clicks Save im trying to perform-some-action and then close the document.<<

    Based on the description, it seems that you want to repurpose the Save action. If you are using Word 2007, you can refer to article below to repurpose command:
    Temporarily Repurpose Commands on the Office Fluent Ribbon

    And since from Word 2010, Office involves backstage view, we can hide the default tab on the backstage view and create a new one to repurpose. You can get more detail from link below:
    Customizing the Office 2010 Backstage View for Developers

    Repurpose SaveAs in Office 2013 Word/Excel/PPT AddIn

    Best regards

    Fei 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 17, 2014 10:08 AM
    Moderator
  • Hi, thanks for your reply.

    Im using word2013. Even when I do the repurpose and then close my doc, Word bombs out!

    Friday, October 17, 2014 10:31 AM
  • Hi Bender,

    Thanks for the detail information however I can't understand exactly about the "Word bombs out". Would you mind share more detail or a screenshot about it to help us understand the exact issue you are handlying?

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 21, 2014 6:39 AM
    Moderator
  • hi,

    thanks for your reply.

    1. I have put the info from the error log in my first post in this thread.

    2. please see the screenshot attached for the error messages from word.

    Thanks for your help and do let me know if you need anything else.

    Tuesday, October 21, 2014 9:14 AM
  • Hi Bender,

    >>Even when I do the repurpose and then close my doc, Word bombs out!<<

    Would you mind sharing the detail infromation how you repupose the save action? Did you remove the DocumentBeforeSave event?

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 29, 2014 10:04 AM
    Moderator
  • i feel we are digressing here, all I want to do is be able to close down my word doc. nothing else. it can be attached to any button/event you like as long as it works.

    at the moment it bombs out for me.

    please advise.

    thanks.

    Friday, October 31, 2014 10:33 AM
  • Hi Bender000,

    According to my experience, such kind of issues are usually caused by the unmanaged resource (for example, windows handle). So I think this issue is not in the code you post but in somewhere allocate the unmanaged resource.  

    Please uncheck the “Enable Just My Code” option to see if we could narrow down this issue.

    If you could provide a sample project to help us reproduce this issue, it will be very helpful for us to troubleshoot it.

    Hope it will help.

    Regards,

    Jeffrey


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, November 4, 2014 6:01 AM
    Moderator
  • Hi

    Thanks for your reply and sorry for the delayed reply.

    Here is a link to my code: http://1drv.ms/1qtjqse

    The code is pretty simple, all i'm trying to do is detect when the user presses "Save", do some stuff and then close the document and close word.

    At the moment when I click the "Save" icon, word crashes out with the error messages described at the top of this post. I am using Word2013 with Windows8.1. I also have Word 210 and Word 2007 installed. This code needs to work in all 3 versions.

    I also tried the ”Enable my code" option to see if this could narrow down this issue, but it didn't help at all.

    Thanks.

    Friday, November 7, 2014 12:10 PM
  • Hi Bender,

    Thanks for you demo project, however I got an error when I open the solution in Visual Studio 2013.

     error  : The project file could not be loaded. Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 1, position 4.  C:\Users\userName\Desktop\mycode\CICOOfficeShared.csproj

    And based on the discussion above, the Word application crashed when you cancel the save action in beforeSave event?

     if I understood correctly, I think beforeSave event is not suitable to cancel the save document action. As a workaround, we could repurpose the Save action. 

    Here are some helpful articles about this topic for your reference:
    Temporarily Repurpose Commands on the Office Fluent Ribbon

    Customizing the Office 2010 Backstage View for Developers

    Repurpose SaveAs in Office 2013 Word/Excel/PPT AddIn

    Regards& Fei 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 10, 2014 9:21 AM
    Moderator