none
Delete .pst in code with outlook open RRS feed

  • Question

  • I'm trying to delete a .pst in code using the outlook object model, problem is outlook keeps it locked until I actually close outlook.

    Sample code:

    Dim oOutlook As Outlook.Application Dim oNS As Outlook.NameSpace
    Dim ofolder As Outlook.MAPIFolder
    oOutlook = New Outlook.Application
    oNS = oOutlook.GetNamespace("MAPI")
    Dim oExpl As Outlook.Explorer = oNS.Application.ActiveExplorer
    oNS.AddStore(fpTempFileFolder & ParseProjectNodeText(prjnode.Text) & ".pst")
    ofolder = oNS.Folders(prjnode.Text).Folders(e.Node.Text)
    oNS.RemoveStore(ofolder)

    'do some stuff with folder

    File.Delete(fpTempFileFolder & ParseProjectNodeText(prjnode.Text) & ".pst")

    'File locked error

    'PS. already added the PSTDisconnectDelay (0 sec, 1 sec)




    • Edited by Rbie Monday, October 7, 2013 2:21 PM
    Monday, October 7, 2013 2:17 PM

Answers

  • Opted for another solution, I'm hiding the .pst and do a cleanup at the next outlook startup since at that point the pst doesn't seem to be locked yet and I'm able to delete it.
    • Edited by Rbie Tuesday, October 8, 2013 9:39 AM
    • Marked as answer by Rbie Tuesday, October 8, 2013 9:39 AM
    Tuesday, October 8, 2013 9:38 AM

All replies

  • By default, PST provider keeps a PST file referenced and loaded for 30 minutes. Or until the PST provider dll itself gets unloaded (e.g. when the host process terminates) and/or when the parent MAPI session is completely destroyed.

    You might want to play with the registry key mentioned in http://support.microsoft.com/Default.aspx?kbid=222328


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, October 7, 2013 2:43 PM
  • If you read my post you would have seen I already did that, and that doesn't work.
    Tuesday, October 8, 2013 5:44 AM
  • If at all possible, do not access that PST file using OOM. You can create a secondary exe that opens and processes the PST file using Extended MAPI (C++ or Delphi) or Redemption. When the secondary exe terminates, your primary executable can delete the PST file.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Tuesday, October 8, 2013 6:04 AM
  • Opted for another solution, I'm hiding the .pst and do a cleanup at the next outlook startup since at that point the pst doesn't seem to be locked yet and I'm able to delete it.
    • Edited by Rbie Tuesday, October 8, 2013 9:39 AM
    • Marked as answer by Rbie Tuesday, October 8, 2013 9:39 AM
    Tuesday, October 8, 2013 9:38 AM