David
David

Reputation: 263

VSTO c# Check if a cell is null

If one has a reference to a cell, how does one check if it is null?

Pseudo Code:

Excel.Range cell = (Excel.Range)MyRange.Cells[1, 1];
if (cell.value2.IsNull) { Stuff }
else { Other Stuff }

Unfortunately, IsNull does not exist.

Upvotes: 3

Views: 14349

Answers (2)

busetto
busetto

Reputation: 1

if (mWSheet1.Cells[z, y + 3].Value2 != null)

or

orderPc.ManufactureDate = Convert.ToString(mWSheet1.Cells[z, y + 3].Value2);

Upvotes: -1

woodykiddy
woodykiddy

Reputation: 6455

You were overthinking. Range.Value2 returns an object. So if you'd like to check for null reference, just do

if(Range.Value2 == null)
{
  //blah blah
}
else
{
  //blah blah
}

You probably should check out the online API documentation a bit more.

http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.value2(v=office.14).aspx

http://msdn.microsoft.com/en-us/library/ms262200(v=office.14).aspx

Upvotes: 9

Related Questions