0

I just started getting a NullReferenceException when trying to set an a range with the Excel interop with the following code:

excelRange.Interior.Color = System.Drawing.Color.Black;

I found that it is actually any color in the System.Drawing.Color class that has this problem. On a different server, the same code works just fine. I have tried removing the reference to System.Drawing and re-adding it but it did not fix the problem.

enter image description here

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
kevlar90
  • 820
  • 2
  • 10
  • 14
  • The right side of the assignment is not the problem. – Erik Philips Mar 06 '16 at 02:51
  • The right side is absolutely the problem. Look at the immediate window, it is the System.Drawing.Color.Black object that has the null reference. I changed the code to excelRange.Interior.Color = 16777216; and it works just fine. I thought for sure it was something to do with the range as well but its not. – kevlar90 Mar 06 '16 at 02:58
  • You may want to repair the .Net Framework. `Color` is a struct, so it cannot be null. `Color.R` is a byte, so it cannot be null. – Erik Philips Mar 06 '16 at 03:05

0 Answers0