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.

Thomas

------------------------------------------------------------------------------
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