How can I run the hosted version of ironpython 1.1 from an Excel add-in?

I'm writing an Excel add-in that hosts IronPython 1.1, and I want to expose an Excel.Application COM object to a PythonEngine instance.

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

Here is my C # code containing 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 the exception mentioned above

0


source to share


2 answers


I believe to install. The value you need to "index" it with the data type; it is more convenient to set .Value2 instead (it can be set directly). So what happens if you use .Value2 instead of .Value in this Python assignment?



+1


source


Nice article with explanation and solution here:

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



Basically, I decided to disable the VSTO proxy that wraps the Excel COM object. It can be done with this line in AssemblyInfo.cs:

[assembly: ExcelLocale1033 (false)]

0


source







All Articles