[python-win32] How to have Excel use a Python callback function

guy lateur guy.lateur at telenet.be
Wed Jul 19 19:08:56 CEST 2006


Hi all,
 
I'm trying to extend the RMB popup menu in Excel to let the user easily
select the project she's working on. The construction of the (hierarchical)
menu is written in a Python COM server. This server is used by some VBA code
in the paricular workbook (a 'timesheet'), in the Workbook_Open() function.
 
The problem is that i can't seem to set the (event-) callback function, ie
what happens when a user selects a given menu item. Below is the code I'd
hoped would work but doesn't:
 
excel = Dispatch("Excel.application")
cbr = excel.CommandBars("Cell")
 
# make a sub-menu
smp = cbr.Controls.Add(Type = constants.msoControlPopup)
smp.Caption = "[BB] Projecten"
smp.Tag = "[BB] Projecten"

# make a menu item
smpo = CastTo(smp, "CommandBarPopup")
nc = smpo.Controls.Add(Type = constants.msoControlButton)
nc.Caption = "Butt"
# callback
nc.OnAction = onProject      # <-- PROBLEM

 
onProject() can be a global function, an instancemethod or a staticmethod,
it always says this: <exceptions.TypeError: Objects of type 'function' can
not be converted to a COM VARIANT>.
 
I did some research and came up with the following strategy:
a. Write a callback class, deriving from some COM interface
b. Convert (an object of) this class to the appropriate 'COM VARIANT'
 
My questions are:
1. Is this the way to do this? If not, what is?
2. If so, what is the interface to inherit from? Functions to override (eg
Invoke())?
3. Do I use pythoncom.WrapObject() for b.? If so, what should be my
gatewayIID & interfaceIID?
 
 
Thank you very much for your time,
g
 
PS: In VBA you have to give it the name of a macro, eg nc.OnAction = Macro1.
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20060719/78d6c3f4/attachment.htm 


More information about the Python-win32 mailing list