How to get my own emailadress from Outlook with python?

Viewed 37

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?

2 Answers

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

Try to use the Logon method or get the Inbox folder prior to getting the user's email address in the code.

Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

If Outlook is not running and you only want to start Outlook with the default profile, don't use the Logon method. A better alternative way is to first, instantiate the Outlook Application object, then reference a default folder such as the Inbox. This has the side effect of initializing MAPI to use the default profile and to make the object model fully functional.

Also I am not sure whether it makes any difference or not, but other samples use upper-case symbols as shown in the following snippet:

import win32com.client as client
outlook = client.Dispatch("Outlook.Application")
Related