I've come across something puzzling about static dispatch. I've used win32com a bit and normally understand what is going on, but I've stubed my toe on something here that I just can't understand. It almost looks like a bug ...
Dynamic dispatch works: (First I go to 'C:\Python24\Lib\site-packages\win32com\gen_py' and remove all the generated '.py') >>> import win32com.client >>> dispatch = win32com.client.Dispatch >>> xlApp = dispatch('Excel.Application') >>> books = xlApp.Workbooks.Open('SomeNew.xls') >>> books <COMObject <unknown>> And if I try to use 'books', no problems: >>> sheet = books.WorkSheets('sheet1') >>> sheet <COMObject <unknown>> Great. So dynamic dispatch works just fine. Except, of course, I can't get the 'constants' and its a bit inefficient, so I tried switching to static dispatch: >>> dispatch = win32com.client.gencache.EnsureDispatch >>> xlApp = dispatch('Excel.Application') >>> books = xlApp.Workbooks.Open('SomeNew.xls') But now, when I look at 'books': >>> books <win32com.gen_py.None.Workbook> which seems to mean 'books' is unusable: >>> books.WorkSheets('channels') Traceback (most recent call last): ... AttributeError: '<win32com.gen_py.Microsoft Excel 11.0 Object Library._Workbook instance at 0x13234048>' object has no attribute 'WorkSheets' The only way around this seems to be: >>> books = win32com.client.Dispatch(books) which means 'books' changes to become: >>> books <win32com.gen_py.Microsoft Excel 11.0 Object Library._Workbook instance at 0x24048544> And therefor is now usable: >>> sheet = books.Worksheets('sheet1') No traceback! So, I've learned (much like a mouse in a maze), that I have to add the line: books = win32com.client.Dispatch(books) and static dispatch will work. What I don't understand is why, under staic dispatch, 'books' becomes this unusable object: <win32com.gen_py.None.Workbook> instead of something useful like either of these two: <COMObject <unknown>> <win32com.gen_py.Microsoft Excel 11.0 Object Library._Workbook instance at 0x13234048> I looked in C:\Python24\Lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x5 which is the folder created by win32com.client.gencache.EnsureDispatch, and, sure enough, it contains all the code for 'Workbook', so its known. And if you look at the AttributeError traceback given above, it correctly identifies the object as: <win32com.gen_py.Microsoft Excel 11.0 Object Library._Workbook instance at 0x13234048> despite claiming that it doesn't have the attribute of that class. Also strange it doesn't seem to happen for all classes of object. For example, if I continue on: >>> sheet = books.Worksheets('sheet1') >>> rng = sheet sheet.Range('A2:B11') >>> rng <win32com.gen_py.Microsoft Excel 11.0 Object Library.Range instance at 0x24277240> In effect, the Range object is correctly identified and useable without having to do this (which I had to do for books): >>> rng = win32com.client.Dispatch(rng) I don't understand the ins and outs of this. I'm left thinking: * there is a bug in the type library suppied which is causing win32com to do something odd * there is a bug in win32com * there's something really important that I don't know and I don't even know where to look. I'm on Windows XP, Python 2.4, win32com ???? (something installed within the last month), Excel 2003 SP1. Any help appreciated. Cheers, Mike _______________________________________________ Python-win32 mailing list Python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32