Zope, COM and Outlook
Hi all! I need to obtain someone's e-mail address from Outlook's default address book. I found a piece of python code in the internet which I modified, and it now looks like this: import codecs, win32com.client def obtainaddress(name): # Create instance of Outlook o = win32com.client.Dispatch("Outlook.Application") mapi = o.GetNamespace("MAPI") folder = mapi.GetDefaultFolder(win32com.client.constants.olFolderContacts) print folder.Item("Igor Leturia").Address o = None obtainaddress('Igor') The code needs win32com, so I installed ActivePython and copied the win32com folder to Zope's lib. I tested the code in Zope's python and it works. But as I need to access it from Zope, I included the function in one of the Zope source modules (User.py to be precise). And when I call it from DTML, it gives me an error: Error Type: com_error Error Value: (-2147221008, 'CoInitialize not called.', None, None) I've put some traces and I know the error is in the first line, o = win32com.client.Dispatch("Outlook.Application"). Any ideas? Thanks in advance, Igor Leturia P.S.: Zope's traceback says: Traceback (innermost last): File C:\Zope\lib\python\ZPublisher\Publish.py, line 150, in publish_module File C:\Zope\lib\python\ZPublisher\Publish.py, line 114, in publish File C:\Zope\lib\python\Zope\__init__.py, line 159, in zpublisher_exception_hook (Object: Zope) File C:\Zope\lib\python\ZPublisher\Publish.py, line 98, in publish File C:\Zope\lib\python\ZPublisher\mapply.py, line 88, in mapply (Object: AAAAA) File C:\Zope\lib\python\ZPublisher\Publish.py, line 39, in call_object (Object: AAAAA) File C:\Zope\lib\python\OFS\DTMLMethod.py, line 130, in __call__ (Object: AAAAA) File C:\Zope\lib\python\DocumentTemplate\DT_String.py, line 473, in __call__ (Object: AAAAA) File C:\Zope\lib\python\DocumentTemplate\DT_Util.py, line 159, in eval (Object: acl_users.getUser('IGOR').getMail()) (Info: acl_users) File <string>, line 2, in f File C:\Zope\lib\python\AccessControl\User.py, line 299, in getMail File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\__init__.py, line 92, in Dispatch File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\dynamic.py, line 81, in _GetGoodDispatchAndUserName File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\dynamic.py, line 72, in _GetGoodDispatch com_error: (see above)
Hi Igor, I worked almost a months on this problem can explain it and tell you how I worked around it - but I've not the real solution: The difference between your Python Test-Program and Zope is, that the test-program probably has only one thread, but Zope works with more than one. As explained in Hammond/Robinson (Python Programming on Win32) you have to initialize each thread with COM. There are differences, if the COM-Object lives in a free-threaded or in a sigle-threaded apartment. for a single-threaded apartment it's like this: Main-Thread: anchor = [COMObject()] object_stream = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, anchor) pyHandle, id = beginthreadex(None, 0, handleRequest, (clientSocket, addr, object_stream), 0) and don't forget to pump the messages ... Each Thread: def handleRequest(clientSocket, addr, object_stream): pythoncom.CoInitialize() anchor = pythoncom.CoGetInterfaceAndReleaseStream(object_stream, pythoncom.IID_IDispatch) anchor = Dispatch(anchor) already, you can use it ... Right now, I haven't found the right place to add this code to ZOPE. So I wrote a little separate process, which does connect to COM and ZOPE just talks with this separate process .... Have fun with it! And please write it to the list, if you find a better solution! Ulla. Igor Leturia schrieb:
Hi all!
I need to obtain someone's e-mail address from Outlook's default address book. I found a piece of python code in the internet which I modified, and it now looks like this:
import codecs, win32com.client
def obtainaddress(name): # Create instance of Outlook o = win32com.client.Dispatch("Outlook.Application") mapi = o.GetNamespace("MAPI") folder = mapi.GetDefaultFolder(win32com.client.constants.olFolderContacts) print folder.Item("Igor Leturia").Address o = None
obtainaddress('Igor')
The code needs win32com, so I installed ActivePython and copied the win32com folder to Zope's lib. I tested the code in Zope's python and it works.
But as I need to access it from Zope, I included the function in one of the Zope source modules (User.py to be precise). And when I call it from DTML, it gives me an error:
Error Type: com_error Error Value: (-2147221008, 'CoInitialize not called.', None, None)
I've put some traces and I know the error is in the first line, o = win32com.client.Dispatch("Outlook.Application").
Any ideas? Thanks in advance,
Igor Leturia
P.S.: Zope's traceback says:
Traceback (innermost last): File C:\Zope\lib\python\ZPublisher\Publish.py, line 150, in publish_module File C:\Zope\lib\python\ZPublisher\Publish.py, line 114, in publish File C:\Zope\lib\python\Zope\__init__.py, line 159, in zpublisher_exception_hook (Object: Zope) File C:\Zope\lib\python\ZPublisher\Publish.py, line 98, in publish File C:\Zope\lib\python\ZPublisher\mapply.py, line 88, in mapply (Object: AAAAA) File C:\Zope\lib\python\ZPublisher\Publish.py, line 39, in call_object (Object: AAAAA) File C:\Zope\lib\python\OFS\DTMLMethod.py, line 130, in __call__ (Object: AAAAA) File C:\Zope\lib\python\DocumentTemplate\DT_String.py, line 473, in __call__ (Object: AAAAA) File C:\Zope\lib\python\DocumentTemplate\DT_Util.py, line 159, in eval (Object: acl_users.getUser('IGOR').getMail()) (Info: acl_users) File <string>, line 2, in f File C:\Zope\lib\python\AccessControl\User.py, line 299, in getMail File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\__init__.py, line 92, in Dispatch File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\dynamic.py, line 81, in _GetGoodDispatchAndUserName File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\dynamic.py, line 72, in _GetGoodDispatch com_error: (see above)
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Igor, You could use an External Method to avoid the threading issues. Joseph On Wed, 28 May 2003 15:29:29 +0200, Ulla Theiss wrote
Hi Igor,
I worked almost a months on this problem can explain it and tell you how I worked around it - but I've not the real solution:
The difference between your Python Test-Program and Zope is, that the test-program probably has only one thread, but Zope works with more than one.
As explained in Hammond/Robinson (Python Programming on Win32) you have to initialize each thread with COM. There are differences, if the COM-Object lives in a free-threaded or in a sigle-threaded apartment.
for a single-threaded apartment it's like this:
Main-Thread: anchor = [COMObject()] object_stream = pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch, anchor) pyHandle, id = beginthreadex(None, 0, handleRequest, (clientSocket, addr, object_stream), 0)
and don't forget to pump the messages ...
Each Thread: def handleRequest(clientSocket, addr, object_stream): pythoncom.CoInitialize() anchor = pythoncom.CoGetInterfaceAndReleaseStream(object_stream, pythoncom.IID_IDispatch) anchor = Dispatch(anchor)
already, you can use it ...
Right now, I haven't found the right place to add this code to ZOPE. So I wrote a little separate process, which does connect to COM and ZOPE just talks with this separate process ....
Have fun with it! And please write it to the list, if you find a better solution!
Ulla.
Igor Leturia schrieb:
Hi all!
I need to obtain someone's e-mail address from Outlook's default address book. I found a piece of python code in the internet which I modified, and it now looks like this:
import codecs, win32com.client
def obtainaddress(name): # Create instance of Outlook o = win32com.client.Dispatch("Outlook.Application") mapi = o.GetNamespace("MAPI") folder = mapi.GetDefaultFolder(win32com.client.constants.olFolderContacts) print folder.Item("Igor Leturia").Address o = None
obtainaddress('Igor')
The code needs win32com, so I installed ActivePython and copied the win32com folder to Zope's lib. I tested the code in Zope's python and it works.
But as I need to access it from Zope, I included the function in one of the Zope source modules (User.py to be precise). And when I call it from DTML, it gives me an error:
Error Type: com_error Error Value: (-2147221008, 'CoInitialize not called.', None, None)
I've put some traces and I know the error is in the first line, o = win32com.client.Dispatch("Outlook.Application").
Any ideas? Thanks in advance,
Igor Leturia
P.S.: Zope's traceback says:
Traceback (innermost last): File C:\Zope\lib\python\ZPublisher\Publish.py, line 150, in publish_module File C:\Zope\lib\python\ZPublisher\Publish.py, line 114, in publish File C:\Zope\lib\python\Zope\__init__.py, line 159, in zpublisher_exception_hook (Object: Zope) File C:\Zope\lib\python\ZPublisher\Publish.py, line 98, in publish File C:\Zope\lib\python\ZPublisher\mapply.py, line 88, in mapply (Object: AAAAA) File C:\Zope\lib\python\ZPublisher\Publish.py, line 39, in call_object (Object: AAAAA) File C:\Zope\lib\python\OFS\DTMLMethod.py, line 130, in __call__ (Object: AAAAA) File C:\Zope\lib\python\DocumentTemplate\DT_String.py, line 473, in __call__ (Object: AAAAA) File C:\Zope\lib\python\DocumentTemplate\DT_Util.py, line 159, in eval (Object: acl_users.getUser('IGOR').getMail()) (Info: acl_users) File <string>, line 2, in f File C:\Zope\lib\python\AccessControl\User.py, line 299, in getMail File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\__init__.py, line 92, in Dispatch File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\dynamic.py, line 81, in _GetGoodDispatchAndUserName File C:\Archivos de programa\Python21\Lib\site-packages\win32com\client\dynamic.py, line 72, in _GetGoodDispatch com_error: (see above)
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
"A living dog is better than a dead lion." Ecclesiastes 9:4
participants (3)
-
Igor Leturia -
Joseph Griffin -
Ulla Theiss