1

I am trying to extract some data from a .pst file using win32com.client as described in the answer of Read PST files from win32 or pypff. However, when I run the code I receive the following error:

File '<'COMObject GetNamespace'>', line 2, in AddStore pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The Outlook data file (.pst) failed to load for this session.', None, 0, -2147467259), None)

The code I used is:

import win32com.client

def find_pst_folder(OutlookObj, pst_filepath) :
    for Store in OutlookObj.Stores :
        if Store.IsDataFileStore and Store.FilePath == pst_filepath :
            return Store.GetRootFolder()
    return None

def enumerate_folders(FolderObj) :
    for ChildFolder in FolderObj.Folders :
        enumerate_folders(ChildFolder)
    iterate_messages(FolderObj)

def iterate_messages(FolderObj) :
    for item in FolderObj.Items :
        print("***************************************")
        print(item.SenderName)
        print(item.SenderEmailAddress)
        print(item.SentOn)
        print(item.To)
        print(item.CC)
        print(item.BCC)
        print(item.Subject)
        count_attachments = item.Attachments.Count
        if count_attachments > 0 :
            for att in range(count_attachments) :
                print(item.Attachments.Item(att + 1).Filename)

Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

pst = r"C:\Users\djmey\Desktop\PSTChecker\MyINBOX.pst"
Outlook.AddStore(pst)
PSTFolderObj = find_pst_folder(Outlook,pst)
try :
    enumerate_folders(PSTFolderObj)
except Exception as exc :
    print(exc)
finally :
    Outlook.RemoveStore(PSTFolderObj)

If anyone has any clue what is causing this or has a good idea of where I should be looking, please let me know.

  • 1
    The error is a generic MAPI_E_CALL_FAILED. Can you open that PST file manually in Outlook (File | Open Outlook Data File)? – Dmitry Streblechenko Jul 07 '21 at 22:34
  • @DmitryStreblechenko Thanks for identifying the error. I have no trouble opening it in Outlook. If it helps, I am able to read it using pypff, although I am running into problems with that as well. – David Meyenberg Jul 08 '21 at 00:34
  • 1
    AS a test, can you use Redemption and its RDOSession.LogonPstStore method? If anything, it should give a better error. – Dmitry Streblechenko Jul 08 '21 at 06:08
  • @DmitryStreblechenko I think I implemented everything correctly. `session = win32com.client.Dispatch("Redemption.RDOSession") pst = r"C:\Users\djmey\Desktop\PSTChecker\INBOX.pst" session.LogonPstStore(pst)` I am now getting this error: File "", line 3, in LogonPstStore pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Redemption.RDOSession', 'Error in IMsgServiceAdmin::ConfigureMsgService: MAPI_E_FAILONEPROVIDER\r\nulVersion: 0\r\nComponent: Outlook Data File\r\nulLowLevelError: 0\r\nulContext: 805700609', None, 0, -2147221219), None) – David Meyenberg Jul 08 '21 at 20:01
  • Have you tried to run that PST file through scanpst.exe? It sure sounds like it is corrupted. – Dmitry Streblechenko Jul 08 '21 at 20:43
  • @DmitryStreblechenko Wasn't able to run my scanpst.exe file. My outlook is buried under Program Files\WindowsApps and the scanpst.exe file wouldn't run. I'm thinking my Office installation may be causing the problem, but you probably have a better idea than me. – David Meyenberg Jul 16 '21 at 00:49
  • It sounds like you have the Windows Store version of Outlook. Uninstall and reinstall it - from the Windows Store - the latest version will install a regular C2R version. – Dmitry Streblechenko Jul 16 '21 at 03:04
  • I got the same error. If I close my Outlook, then it works correctly. In my case, if I want to use this code I cannot have outlook open. – f4d0 Jul 18 '22 at 17:59

0 Answers0