Home > database >  Release file lock on PST files
Release file lock on PST files

Time:09-29

I have two functions in VB that I use for archiving my emails on completed projects. The first opens either all of my previous year email stores, or just one at a time. These PST files are stored on Dropbox, so as soon as Outlook opens the PST files, it locks them and won't let Dropbox sync the files. Right now, I have a third routine that closes all open PST files, shuts Outlook down and calls a batch file that restarts outlook without the PST files open so that Dropbox can finish syncing.

Sub OPENALL()
    Application.GetNamespace("MAPI").AddStore "E:\2012.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2013.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2014.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2015.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2016.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2017.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2018.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2019.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2020.pst"
    Application.GetNamespace("MAPI").AddStore "E:\2021.pst"
End Sub

Sub CLOSEMSGSTORE()
    Dim objStores As Outlook.Stores
    Dim objStore As Outlook.store
    Dim objOutlookFile As Outlook.folder
    Dim i As Integer
 
    'Get All Outlook Files in Your Outlook
    Set objStores = Outlook.Session.Stores
 
    For i = objStores.Count To 1 Step -1
 
        Set objStore = objStores.item(i)
        Set objOutlookFile = objStore.GetRootFolder
 
        'Exclude the Outlook OST File for Exchange mailbox
        If objStore.ExchangeStoreType = olNotExchange Then
           'Close the PST File
           Outlook.Session.RemoveStore objOutlookFile
        End If
    Next
 
    Set objStores = Nothing
    Set objStore = Nothing
    Set objOutlookFile = Nothing
    
    Shell ("C:\Users\csalv\OneDrive\Documents\Computer Tweaks\outrestart.bat")
    Application.Quit
End Sub

Is there any way to force the file lock on the PST file to release without having to shut Outlook down? Thanks.

CodePudding user response:

By default, the MSUPST 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).

You might want to play with the registry key mentioned in https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/222328

Another solution that will always work is to wrap your PST processing functionality into a separate exe that does not use Outlook Object Model (e.g. you can use Redemption and its RDOSession.LogonPstStore method to open PST files) and launch it from your main executable. When the auxiliary process exits, your main executable should be able to manipulate the PST file.

  • Related