none
NullReferenceException adding new Sheet in Excel 2010 during SheetActivate(Object sh) event RRS feed

  • Question

  • Hi there,

    Im developping a Document-Level Add-in using .NET 4.0, VSTO 2010, and C# for Excel 2010.

    The class This_Workbook has a field   Microsoft.Office.Tools.Excel.WorkSheet currentSheet, in order to have accessible the activate sheet of the book.

    Then I have an eventHandler as following:

     
    private void ThisWorkbook_SheetActivate(Object sh)
     {   
                if (sh != null)
                {
                    Excel.Worksheet ws = sh as Excel.Worksheet;
                    this.currentSheet = Globals.Factory.GetVstoObject(ws as Excel.Worksheet);
                 } 
     }


    The problem comes up when I try to create a new sheet using the standar Excel graphical Interface, a NullReferenceException arise, and I dont know why cause Im managing when the object sh is null.

    I would appreciate if someone could help me.

    Thanks in advance.

    coco.

     

    Monday, March 5, 2012 4:51 PM

Answers

  • Hi cocoVSTO,

    Thanks for posting in the MSDN Forum.

    I can reproduce your issue on my side. It seems only happen at a new sheet which you add in you document-level application. Because the worksheet which you access via GetVstoObject must be declared as a property of [your project name].Globals class. You can try this scenario:

    1. creat a new worksheet on solution explorer via right click mouse on xlsx file and access it use GetVstoObjcet when your project run. You will find it works fine.

    I hope it can help you.

    Have a good day,

    Tom


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

    • Marked as answer by cocoVSTO Monday, March 12, 2012 9:34 PM
    Monday, March 12, 2012 4:06 AM
    Moderator

All replies

  • If you are developing your solution then in exception there should be information about exact line in code where exception happened. Can you verify this?
    Tuesday, March 6, 2012 10:05 AM
  • Of course, sorry I forgot to mention it.

    The line in the code that arise the exception is:

     this.currentSheet = Globals.Factory.GetVstoObject(ws as Excel.Worksheet);

    Method Globals.Factory.GetVstoObject(...) produce the exception but I dont understand why

    cause the ws object is not null, or at least I believe is not null.

    Tuesday, March 6, 2012 4:20 PM
  • change your code to not cast using 'as' but as a direct cast like: Excel.Worksheet ws = (Excel.Worksheet)sh; and see what happens
    Wednesday, March 7, 2012 8:15 AM
  • I have already tried this, but the exception still happens...

    Wednesday, March 7, 2012 10:41 AM
  • I have been doing some tests and I have realized that when I create a new sheet using the Excel Graphical Interface, everytime I try to use the method Globals.Factory.GetVstoObject(ws) with the new sheet, the exception arises.

    I really dont know what is happening...

    Wednesday, March 7, 2012 10:53 AM
  • Hi cocoVSTO,

    Thanks for posting in the MSDN Forum.

    I can reproduce your issue on my side. It seems only happen at a new sheet which you add in you document-level application. Because the worksheet which you access via GetVstoObject must be declared as a property of [your project name].Globals class. You can try this scenario:

    1. creat a new worksheet on solution explorer via right click mouse on xlsx file and access it use GetVstoObjcet when your project run. You will find it works fine.

    I hope it can help you.

    Have a good day,

    Tom


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

    • Marked as answer by cocoVSTO Monday, March 12, 2012 9:34 PM
    Monday, March 12, 2012 4:06 AM
    Moderator
  • Hi Tom,

    Thank you for answering.

    You are right, I just can access worksheets previously created during design time.Its a pitty cause I had something different planned for my app, but I think is the only way.

    You have been very helpful.

    Thank you again.

    coco.

    Monday, March 12, 2012 9:34 PM