Processing "No cells were found." Error in Excel

I am working on an Excel VSTO application and detect cell cells in worksheets using the code below

Excel.Range rngTemp;
Excel.Range rngErrorRange;

Excel._Worksheet Sheet1 = (Excel._Worksheet)xlCTA.Sheets["Sheet1"];
rngTemp = wsCTAWK11.UsedRange;
rngErrorRange = rngTemp.SpecialCells(Excel.XlCellType.xlCellTypeFormulas, Excel.XlSpecialCellsValue.xlErrors);

when the error cells are actually detected, I have no problems, but when I have no error cells in this sheet, I get the following exception.

**threw an exception of type 'System.Runtime.InteropServices.COMException'
    base {System.Runtime.InteropServices.ExternalException}: {"No cells were found."}**

How to handle this ... Pls Help

+3
source share
3 answers

Catch the exception and handle it as you want?

+1
source
try
{
    Excel.Range rngTemp;
    Excel.Range rngErrorRange;

    Excel._Worksheet Sheet1 = (Excel._Worksheet)xlCTA.Sheets["Sheet1"];
    rngTemp = wsCTAWK11.UsedRange;
    rngErrorRange = rngTemp.SpecialCells(Excel.XlCellType.xlCellTypeFormulas,
Excel.XlSpecialCellsValue.xlErrors);
}
catch (System.Runtime.InteropServices.COMException ex)
{
    //Handle here
}
+1
source

SpecialCells , myRange.SpecialCells .

:

 public static Range SpecialCellsCatchError(this Range myRange, XlCellType cellType)
    {
        try
        {
            return myRange.SpecialCells(cellType);
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            return null;
        }
    }

null, .

0

Source: https://habr.com/ru/post/1749564/


All Articles