-1

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?

Robert
  • 159
  • 1
  • 9
  • shouldn't it be uppercase "Outlook.Application"? – brianangulo Sep 10 '22 at 07:20
  • Is Outlook running at the time of the call? Is either app running with elevated privileges? – Dmitry Streblechenko Sep 10 '22 at 15:32
  • `outlook.Session.Accounts.Item(1).DisplayName` works for me – DS_London Sep 10 '22 at 15:57
  • @DmitryStreblechenko It could be that outlook is running, I can't control whether Outlook is running or not because the program I write will use by many people. Evertime the app will started it checks the User Emailadresse for verification. Every user has a specific address and has to check before they can use it. I do this to get sure that the software only can execute in the company. – Robert Sep 10 '22 at 16:11
  • @Robert - I understand that you cannot control the environment, just trying to narrow down the problem. So is it when the error is raised? How about elevated privileges? – Dmitry Streblechenko Sep 10 '22 at 17:28
  • The error is raised when outlook is not running. With a running outlook i didn't try. What do you mean with elevated privileges? – Robert Sep 10 '22 at 20:34

2 Answers2

1

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

Robert
  • 159
  • 1
  • 9
0

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")
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45