[IronPython] Excel events from IronPython?
Dino Viehland
dinov at exchange.microsoft.com
Thu May 24 19:44:33 CEST 2007
I think this is what you want:
import clr
clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c')
import Microsoft.Office.Interop.Excel as Excel
ex = Excel.ApplicationClass()
from System.Reflection import Missing
xlBook = ex.Workbooks.Add( Missing.Value )
xlSheet1 = xlBook.Worksheets[1]
xlSheet1.Activate()
def foo(*args): print args
Excel.DocEvents_Event.add_Change(xlSheet1, foo)
This is a really odd way to do this but there's multiple types we can find that are associated with the COM object. Currently we're picking just one of the COM interfaces which is supported on the object so to use the other interfaces you need to call directly through the interface.
In this case if you look at the Worksheet class in MS.Office.Interop.Excel you'll see it implements:
MS.Office.Interop.Excel_Worksheet
MS.Office.Interop.Excel.DocEvents_Event
We're picking _Worksheet as the interface type instead of Worksheet.
-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Christian Muirhead
Sent: Thursday, May 24, 2007 6:11 AM
To: Discussion of IronPython
Subject: [IronPython] Excel events from IronPython?
Hi -
I'm automating Excel from IP, which has been a bit painful, but looks to
be working now. One extra thing I'd like to do is to subscribe to a
worksheet's change event.
This (http://support.microsoft.com/kb/302815/) is an article that does
in C# essentially what I'd like to do in IP. I can construct an
AppEvents_SheetChangeEventHandler delegate wrapped around a function,
but I can't find the event to hook it up - there's no .Change event on
the worksheet.
Nosing around the Microsoft.Office.Interop.Excel namespace (the Excel
object model documentation is pretty patchy, although I've looked there
too), I can see a type called AppEvents_Event, which does seem to expose
the events I want. But I can't work out how to instantiate it (or even
whether I should be trying to), and trying to attach the delegate to the
event on the type results in:
>>> Excel.AppEvents_Event.SheetChange +=
Excel.AppEvents_SheetChangeEventHandler(func)
Traceback (most recent call last):
File , line 0, in <stdin>##165
StandardError: Non-static method requires a target.
(Which is pretty clear.)
I'm about to try following the example in C#, and hopefully I'll be able
to work out what the C# code's doing differently.
In the meantime, has anyone successfully consumed Excel (or any Office
application) events in IronPython? Can anyone familiar with COM<->.NET
interop see what I should be doing?
Thanks,
Christian
--
Christian Muirhead
Resolver Systems
christian.muirhead at resolversystems.com
Office address: 17a Clerkenwell Road, London EC1M 5RD, UK
Registered address: 843 Finchley Road, London NW11 8NA, UK
Resolver Systems Limited is registered in England and Wales as company
number 5467329.
VAT No. GB 893 5643 79
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
More information about the Ironpython-users
mailing list