Search code examples
pythonoutlookwin32comoffice-automation

Python: How to get my own emailadress from Outlook?


I try to get the users emailadress from outlook with python but i allways get this Error:

  File "C:\Users\me\Documents\Coding\Python_Projects\TEST\mainLogin.py", line 76, in __init__
    self.myAdress = self.outlook.Session.CurrentUser.Address
      File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\__init__.py", line 485, in __getattr__
        return self._ApplyTypes_(*args)   File "C:\Users\me\AppData\Local\Programs\Python\Python39\lib\site-packages\win32com\client\__init__.py", line 478, in _ApplyTypes_
        self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147467260, 'Vorgang abgebrochen', None, None)

I try it with this code:

import win32com.client as win32
...
...
self.outlook = win32.gencache.EnsureDispatch('outlook.application')
self.myAdress = self.outlook.Session.CurrentUser.Address

Can anybody tell me, what is wrong here?


Solution

  • I solved the problem this way:

    import win32com.client as win32
    ...
    ...
    self.outlook   = win32.gencache.EnsureDispatch('outlook.application')
    self.item      = self.outlook.CreateItem(0)
    self.myAddress  = self.item.Session.CurrentUser.Address
    

    self.myAddress now stores my Email address.

    It doesn't matter whether you use Upper-case or Lower-case for

    ('outlook.application')

    I tried it with both and all what i got was identically.

    If this solution is not good or can make any problems please let me know. For now it works for my. Thank You