On Tuesday 21 September 2010 20:11:08 Thomas Heller wrote:
> Am 21.09.2010 12:34, schrieb David Naylor:
> > Hi,
> > 
> > I'm trying to use multiple instances of Excel with comtypes.  Ideally I
> > would
> > 
> > like to:
> >  - iterate over all active instances of Excel
> >  - get a Workbook using its (prior) known name (the Workbook would have
> >  been
> > 
> > opened in one of the excel instances).
> > 
> > Unfortunately CreateObject creates a new instance of Excel and
> > GetActiveObject returns the first instance of Excel, which invariably
> > does not have the required workbook.
> > 
> > My searches for a solution came up with BindToMoniker which will allow me
> > to get a workbook (solves the second problem) but I cannot figure out
> > how to get that to work in comtypes.
> 
> Here is a somewhat hackish version that works without defining the IMoniker
> interface in comtypes:
> 
> """
> from comtypes import IUnknown, POINTER, byref, oledll
> from comtypes.client import wrap
> 
> def BindToFile(path):
>     moniker = POINTER(IUnknown)()
>     oledll.ole32.CreateFileMoniker(unicode(path),
>                                    byref(moniker))
>     result = POINTER(IUnknown)()
>     oledll.ole32.BindMoniker(moniker,
>                              0,
>                              byref(result._iid_),
>                              byref(result))
>     return wrap(result)
> 
> if __name__ == "__main__":
>     import sys
>     print BindToFile(sys.argv[1])
> """
> 
> It prints
> <POINTER(_Workbook) ptr=0x298724 at 3248cb0>
> if called with a path of an xls file.

Thanks, that works.  One down side is it does not report if the file was not 
open already.  A minor issue.  

I've noticed one cannot attach events to an Excel Worksheet and there does not 
appear to be a comtypes.gen.Excel.WorksheetEvents class.  Is this expected?  I 
remember being able to use events under win32com.  

Is there anyway to get python to exit from within an event and to get 
PumpEvents to terminate early?

Thanks you for your solution.  

David

Attachment: signature.asc
Description: This is a digitally signed message part.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
comtypes-users mailing list
comtypes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/comtypes-users

Reply via email to