Excel Interop: Range.FormatConditions.Add throws MissingMethodException
- by Zach Johnson
I am writing an application which uses the Microsoft.Office.Interop.Excel assembly to export/import data from Excel spreadsheets. Everything was going fine (except for 1 based indexing and all those optional parameters!), until I tried to use conditional formatting. When I call Range.FormatConditions.Add I get a MissingMethodException telling me that no such method exists. This happens in both Vista and XP.
Here's an example of the code that generates the exception:
//1. Add a reference to Microsoft.Office.Interop.Excel (version 11.0.0.0)
//2. Compile and run the following code:
using Microsoft.Office.Interop.Excel;
class Program
{
static void Main(string[] args)
{
Application app = new Application();
Workbook workbook = app.Workbooks[1];
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
Range range = worksheet.get_Range("A1", "A5");
FormatCondition condition = range.FormatConditions.Add(
XlFormatConditionType.xlCellValue,
XlFormatConditionOperator.xlBetween,
100,
200);
}
}