views:

275

answers:

2

I am writing an Excel add-in that hosts IronPython 1.1 and I want to provide the Excel.Application COM object to the PythonEngine instance.

My C# can access members of the COM object just fine. However, when my IronPython script accesses members of the COM object, I get a "System.ArgumentException: Object of type 'System.Int32' cannot be converted to type 'System.UInt32&'."

Here is my C# code hosting IronPython 1.1:

public void ExecuteFile(string path) {
    // see if COM object works
    Debug.WriteLine(Globals.ThisAddIn.Application.ActiveWindow.Caption); 

    engine.Globals.Add("excel", Globals.ThisAddIn.Application);

    try
    {
        engine.ExecuteFile(path);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
    }
}

and here is my IronPython test script:

excel.ActiveSheet.Range['A1'].Value2 = 42 // throws exception mentioned above

+1  A: 

I believe that to set .Value you need to "index" it with the datatype; it's more convenient to set .Value2 instead (it can be set directly). So what happens if you use .Value2 instead of .Value in that Python assignment?

Alex Martelli
A: 

A good article with explanations and a solution is here:

http://codeidol.com/csharp/c-sharp-in-office/Working-with-Excel-Objects/Special-Excel-Issues/

Basically, I opted for disabling the VSTO proxy which wraps the Excel COM object. It can be done with this line in AssemblyInfo.cs:

[assembly: ExcelLocale1033(false)]

Mads