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.

8
  • 1
    The error is a generic MAPI_E_CALL_FAILED. Can you open that PST file manually in Outlook (File | Open Outlook Data File)? Commented Jul 7, 2021 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. Commented Jul 8, 2021 at 0:34
  • 1
    AS a test, can you use Redemption and its RDOSession.LogonPstStore method? If anything, it should give a better error. Commented Jul 8, 2021 at 6: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 "<COMObject Redemption.RDOSession>", 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) Commented Jul 8, 2021 at 20:01
  • Have you tried to run that PST file through scanpst.exe? It sure sounds like it is corrupted. Commented Jul 8, 2021 at 20:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.