Clear SecureTempFolder automatically using VBA

Last reviewed on July 15, 2012

Applies to Microsoft Outlook 2010, Outlook 2007, Outlook 2003, Outlook 2002, Outlook 2000

In Outlook SecureTemp Files Folder and Red X's in Email Messages I explain what the SecureTemp folder is and issues that result from a "full" SecureTemp folder. I also tell you how to find the folder and empty it manually.

While you can delete the contents of the SecureTempFolder manually, you may want to delete the folder each time you close Outlook.

If you prefer to run this manually whenever you feel like it, change Private Sub Application_Quit() to Public Sub EmptySecureTemp() and run it as needed.

To use, open the VBA editor using Alt+F11 then add the code to ThisOutlookSession.

Reprinted with permission of Peter Marchert

Option Explicit


Private Sub Application_Quit()
'=====================================================================
' Deletes the files of the SecureTempFolder (OLK) when closing Outlook
' (c) Peter Marchert - http://www.outlook-stuff.com
' 2008-11-06 Version 1.0.0
'=====================================================================
Dim objFSO As Object
Dim objWsh As Object
Dim objFolder As Object
Dim strRegKey As String
Dim strOLK As String

On Error Resume Next
'---------------------------------------------------------------------
' To read data from the registry
'---------------------------------------------------------------------

Set objWsh = CreateObject("WScript.Shell")

'---------------------------------------------------------------------
' Set the registry key to read
'---------------------------------------------------------------------

strRegKey = "HKCU\Software\Microsoft\Office\%.0\Outlook\Security\OutlookSecureTempFolder"

'---------------------------------------------------------------------
' Read SecureTempFolder from the registry
'---------------------------------------------------------------------

Select Case Left(Outlook.Version, 2)
Case "9.": strOLK = objWsh.RegRead(Replace(strRegKey, "%", "9"))
Case "10": strOLK = objWsh.RegRead(Replace(strRegKey, "%", "10"))
Case "11": strOLK = objWsh.RegRead(Replace(strRegKey, "%", "11"))
Case "12": strOLK = objWsh.RegRead(Replace(strRegKey, "%", "12"))
Case "14": strOLK = objWsh.RegRead(Replace(strRegKey, "%", "14"))
Case Else
MsgBox "Cannot determine your Outlook version.", vbCritical + _
vbOKOnly, "Delete OLK"
Exit Sub

End Select

'---------------------------------------------------------------------
' VBA does not provide comfortable functions to delete files, so we use
' VB-Script.
'---------------------------------------------------------------------

Set objFSO = CreateObject("Scripting.FileSystemObject")

'---------------------------------------------------------------------
' Delete all files in the SecureTempFolder (True = force deleting)
'---------------------------------------------------------------------

Call objFSO.DeleteFile(strOLK & "*.*", True)

'---------------------------------------------------------------------
' Reference the SecureTempFolder
'---------------------------------------------------------------------

Set objFolder = objFSO.GetFolder(strOLK)

'---------------------------------------------------------------------
' Open the folder if it is not empty
'---------------------------------------------------------------------

If objFolder.Files.Count Then Call Shell("explorer.exe " & strOLK)

'---------------------------------------------------------------------
' Clean Up
'---------------------------------------------------------------------

Set objFolder = Nothing
Set objFSO = Nothing
Set objWsh = Nothing
End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Please post long or more complicated questions at Outlookforums.

Leave a Reply

If the Post Coment button disappears, press your Tab key.