winapioutlookwin32comcom-automation

Problem with win32com while trying to call .GetFirst() function in outlook


I use win32com library to interact with outlook and can`t see the relationsheep between the exception appearence and something else. Exception appears not every time i run the code. I will approciate if you give me some links to clear documentation on this lib or try to help me with problem

Exception appeared: (-2147023170, 'The remote procedure call failed.', None, None)

Traceback (most recent call last):

  File "C:/.../sources/main.py", line 60, in main

    logger=logger)

  File "Outlook_API_v1.py", line 142, in save_mail

    message = messages.GetFirst()

  File "C:\...\AppData\Local\Temp\9\gen_py\3.7\00062FFF-0000-0000-C000-000000000046x0x9x6\_Items.py", line 53, in GetFirst

    ret = self._oleobj_.InvokeTypes(86, LCID, 1, (9, 0), (),)

pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)

Solution

  • Outlook may not hang up in memory serving your needs. The process can be closed. In that case you may see the described error in the code.

    To prevent this from happening I'd suggest adding a new Explorer instance to the Explorers collection and keeping it all alive until you are done. The Explorers.Add method creates a new instance of the explorer window. Don't worry about the window, the explorer window is initially hidden. You must call the Display method of the Explorer object to make it visible if required. For example, the following VBA code shows how to use this method:

    Sub DisplayDrafts() 
     Dim myExplorers As Outlook.Explorers
     Dim myOlExpl As Outlook.Explorer 
     Dim myFolder As Outlook.Folder 
     
     Set myExplorers = Application.Explorers
     Set myFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderDrafts) 
     
     Set myOlExpl = myExplorers.Add(myFolder, olFolderDisplayNoNavigation) 
     
     myOlExpl.Display 
     
    End Sub
    

    Note, Outlook is a singleton. And if you try to automate it while a user is working with the application and then close it you may trap into the same scenario.