views:

1112

answers:

2

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);
    }
}
+2  A: 

This may be a good link for you: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/8a91d154-f766-427a-963c-16dfa39e154a/

Gavin Miller
Thanks! That link solved the problem. I was able to use reflection to invoke the method and now everything works great!
Zach Johnson
A: 

The Add() method for FormatConditions interface has 8 parameters, not 4. AFAIK there is no extension method in Excel.Extensions namespace for this type. Did you use an extension method in the example above or ...?

Anonymous Type
I haven't worked with Excel interop lately, but IIRC the method signature for `Add` changed to 8 parameters in version 12 of the Excel interop libraries. Office 2007 allows access through version 11. In version 11 the signature is 4 parameters. Compiling against version 11 (for backward compatibility) forces you to use 4 parameters, but does not actually work at runtime. That was the error I was experiencing. Using reflection as shown by the link Gavin Miller provided solved the problem.
Zach Johnson