Visual Studio Developer Center > Visual Studio Forums > Visual Studio Tools for Office > word crashes when we try to either file save(Ctrl +S) or programmatically save word.save() in the presence of our COM addin
Ask a questionAsk a question
 

Questionword crashes when we try to either file save(Ctrl +S) or programmatically save word.save() in the presence of our COM addin

  • Thursday, November 05, 2009 3:34 AMKaarthikeyan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi ppl,

    We have a COM addin for word called Documentum Application connectors. In some machines we find that word crashes whenever our addin is enabled and when we try a File save (CTRL + S) or programmatically attempt save in our code using the API word.save() we get the following exception in our log

    System.NullReferenceException: Object reference not set to an instance of an object  at Microsoft.Office.Interop.Word.DocumentClass.Save().

    Can somebody let us know what could be the possible reasons this API as well as normal File save is leading to a crash in word. This happens in few machines.
    Please help urgently needed.

All Replies

  • Thursday, November 05, 2009 7:11 AMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This means an Object that you access isn't available anymore.
    Maybe you called document.ReleaseCOMObject somewhere in your code.
    Then the document variable is gone.

    Maybe show us some code - e.g. where you call the document.save

    Greets - Helmut


    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Thursday, November 05, 2009 11:20 AMKaarthikeyan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your response Helmut.

    We have an  ApplicationEvents2_DocumentBeforeSaveEventHandler event handler where we perform necessary actions before saving our document locally. Here we finally release the comobject. Here is a portion of code..

    someMethod()
    {
    ...........
    ...........
    document.save();
    }

    private void DocumentBeforeSave(MSWord.Document doc, ............. )
    {
       try
        {
           ..............
        }
       catch(Exception e)
        {


         }
         finally
         {
                if (doc != null)
                {
                      logger.debug("before release");
                      Marshal.ReleaseComObject(doc);
                      logger.debug("after release");
                }
          }
    }

    We could see that "After release" is getting printed but immediately after we hit upon the
    System.NullReferenceException: Object reference not set to an instance of an object  at Microsoft.Office.Interop.Word.DocumentClass.Save() exception.

    We even tried removing/commenting the entire Finally block but still Word crashes. So we believe this is not due to the Release of com object.

    Please let us know your thoughts on this.

  • Thursday, November 05, 2009 2:48 PMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The DocumentBeforeSave event is accurately named: it triggers before the file is saved. So you're releasing it before the Save method can finish saving - thus the error message. It's also questionable that you should be releasing an object provided by the event parameters - that object does not "belong" to you.

    Please note that this forum (officially) supports only VSTO Add-ins. With a VSTO add-in you do not release COM objects - the VSTO technology takes care of that.

    Even so, I can see no reason for releasing the document within the BeforeSave event. Instead, if you really need to do this, then do it after the document.save() call in SomeMethod().
    Cindy Meister, VSTO/Word MVP
  • Thursday, November 05, 2009 4:09 PMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Seconded.

    Inthis eventhandler you get a reference to the Document Instance.
    Don't call ReleaseCOMObject.

    I don't know your code - but:
    I assume that you call another Method inside your BeforeSave Method, where you pass the Document as variable and also do a ReleaseCOMObject there.

    Search your Code for ReleaseCOMObject and comment that out.

    Hope this helps,
    greets - Helmut

    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Thursday, November 05, 2009 7:30 PMKaarthikeyan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Cindy/Helmut,

    I can positively assure you that i had commented/removed the ReleaseCOmobject from the methods in question (Documentbefore save event handler and method which invokes it) and still the same effect -Word crashes. Please note the above happens when doing programmatically with Document.save().

    The same is the case when doing Ctrl + S i.e file save normally also . However this does not happen in all the machines it happens in only few machines .
    One thing is however clear is that the ReleaseCOm object is not the reason for this crash.

    So please let me know your thoughts on what else might contribute to this word crash when invoking Document.save() or File->save.

    One pointer is that we could see documentbeforesave handler getting executed without problem , it is after this event handler that is at the point of actual saving does this crash and subsequently the System.NullReferenceException: Object reference happens.

    Am flummoxed still on why this happens on certain environment . Do suggest

    Thanks a lot
    Kaarthikeyan
  • Thursday, November 05, 2009 9:00 PMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Suggestion:

    Comment out the complete DocumentBeforeSave Method.
    Does that fix the Issue?

    Greets - Helmut
    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Friday, November 06, 2009 1:52 AMKaarthikeyan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Unfortunately Helmut,

    DocumentBefore save eventhandler is a necessity for our addin and cannot be commented. But from debugging we could see it is after the DocumentbeforeSave eventhandler that we are hitting this exception and word crashes.

    Am not able to think as to what may contribute to the crash or System.NullReferenceException: Object reference especially since we do not releaseComobject.

    However one other observation is the Document object continues to be not null. But it is when we access some of its property like Document.name etc we hit System.NullReferenceException: Object reference does this provide you guys any clue?



    Kaarthikeyan
  • Friday, November 06, 2009 7:23 AMHelmut ObertannerMVP, AnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    HI,

    I know that it is a requirement for your Solution.
    But when you comment out the Method - and  you didn't get an Exception - then you know that somethings going Wrong here.

    You set the "Document to null" at some point and this is why you get a NullRef exception.

    So - first try to find out at wich point it is set to null.
    Attach with the Debugger.
    Under Exceptions enable all CLR Exceptions.
    When it comes to the NullRefException your Debugger should halt.
    Then go back in the callstack and you should see where your Document is set to null.

    Maybe that would help.

    Greets - Helmut
     
    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
  • Friday, November 06, 2009 7:48 AMCindy MeisterMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Unfortunately Helmut,

    DocumentBefore save eventhandler is a necessity for our addin and cannot be commented. But from debugging we could see it is after the DocumentbeforeSave eventhandler that we are hitting this exception and word crashes.

    Am not able to think as to what may contribute to the crash or System.NullReferenceException: Object reference especially since we do not releaseComobject.

    However one other observation is the Document object continues to be not null. But it is when we access some of its property like Document.name etc we hit System.NullReferenceException: Object reference does this provide you guys any clue?


    Please run a test, doing as Helmut suggested (not letting DocumentBeforeSave execute). This will tell us whether the problem is occurring due to something happening during this event, or whether the problem is elsewhere (perhaps in the machine configuration).
    Cindy Meister, VSTO/Word MVP