none
Excel Interop exception unhandled RRS feed

  • Question

  • I put the following inside a button for testing purposes.

    private void button2_Click(object sender, EventArgs e)
        {
            //Get active excel instance
    
            Excel.Application xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    
            //Get active excel workbook
    
            Excel.Workbook xlWorkBook = (Excel.Workbook)xlApp.ActiveWorkbook;
    
            //Get active excel worksheet
    
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;
    
            //Get value for excel
    
            string AValue = xlApp.ActiveCell.Value2.ToString();
    
            //Return value to winform textbox
            txtSearchPattern.Text = AValue;
        }

    This appeared to work the first time and then I started getting a message on the line Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet; saying that NullReferenceException was Unhandled. I did some searching on this error but have not understood how to fix this in my code.

    What am I doing wrong and how do I fix it?

    Monday, May 2, 2016 2:09 PM

Answers

  • >>>This appeared to work the first time and then I started getting a message on the line Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet; saying that NullReferenceException was Unhandled. I did some searching on this error but have not understood how to fix this in my code.

    According to your description, I have made a sample to try to reproduce this issue, unfortunately, I can not. So I suggest that you could open workbook with code to check if this Excel file is corrupt:
    Excel.Application xlApp = new Excel.Application();
    
    //Excel file path
    xlApp.Workbooks.Open("your Excel file path");
    
    //Get active excel workbook
    
    Excel.Workbook xlWorkBook = (Excel.Workbook)xlApp.ActiveWorkbook;
    
    //Get active excel worksheet
    
    Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;

    In addition you could refer about Troubleshooting Exceptions: System.NullReferenceException

    • Marked as answer by David_JunFeng Thursday, May 12, 2016 5:07 AM
    Tuesday, May 3, 2016 2:39 AM
  • Hi, jrdnoland

    Have you solved your issue? if have, please mark the reply as an answer if you find it is helpful.

    Thanks for your understanding.
    • Marked as answer by jrdnoland Sunday, May 15, 2016 3:35 PM
    Thursday, May 5, 2016 6:03 AM

All replies

  • >>>This appeared to work the first time and then I started getting a message on the line Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet; saying that NullReferenceException was Unhandled. I did some searching on this error but have not understood how to fix this in my code.

    According to your description, I have made a sample to try to reproduce this issue, unfortunately, I can not. So I suggest that you could open workbook with code to check if this Excel file is corrupt:
    Excel.Application xlApp = new Excel.Application();
    
    //Excel file path
    xlApp.Workbooks.Open("your Excel file path");
    
    //Get active excel workbook
    
    Excel.Workbook xlWorkBook = (Excel.Workbook)xlApp.ActiveWorkbook;
    
    //Get active excel worksheet
    
    Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.ActiveSheet;

    In addition you could refer about Troubleshooting Exceptions: System.NullReferenceException

    • Marked as answer by David_JunFeng Thursday, May 12, 2016 5:07 AM
    Tuesday, May 3, 2016 2:39 AM
  • Thank you David, I will read the article and try this on another computer.
    Tuesday, May 3, 2016 7:48 AM
  • Hi, jrdnoland

    Have you solved your issue? if have, please mark the reply as an answer if you find it is helpful.

    Thanks for your understanding.
    • Marked as answer by jrdnoland Sunday, May 15, 2016 3:35 PM
    Thursday, May 5, 2016 6:03 AM