[IronPython] COM Interop: Why does this code only work in IronPython Console?

Kelie kf9150 at gmail.com
Wed Sep 12 19:29:00 CEST 2007


Hello,

The following code works when typed into IronPython console. But when
I save the code into a file named excel.py and run ipy.exe excel.py,
the event handling does not seem to work. Nothing happens when a new
workbook is created.

Any suggestion? Thanks a lot.

import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel
clr.AddReference('System.Windows.Forms')
import System.Windows.Forms.MessageBox as MessageBox

def getActiveObject(appId):
	from System.Runtime.InteropServices import Marshal
	return Marshal.GetActiveObject(appId)
	
def getActiveExcelApplication():
	return getActiveObject("Excel.Application")

def onNewWorkbook(workbook):
    print "Creating a new workbook..."
    MessageBox.Show("Creating a new workbook...")

xlApp = getActiveExcelApplication()
MessageBox.Show(xlApp.ActiveWorkbook.Name) #This line works.
Excel.AppEvents_Event.add_NewWorkbook(xlApp, onNewWorkbook) #This line
does not work.

-- 
Kelie



More information about the Ironpython-users mailing list