Share via


Deleting Outlook Temporary Files

MS Outlook is capable of displaying files attached to messages quite well. But what happens when the attachment file is saved in Outlook, and then sent to a recipient? What happens if one file is being processed?

Although it is better to first save a file on disk in a specified folder before processing it, and then modify it outside a mail client - as this gives us control over the file - users frequently behave in the way described in this article. When opening and modifying a file, Outlook doesn't save it directly in Outlook.pst, but in a temporary folder. This is a collision-free process if a user applies it occasionally. If there are many files in a folder, Outlook creates a new folder.

Disk cleaning doesn't empty these folders, although copies of these files have been placed in the "Sent Items" folder, which makes our disk and system overloaded. Deleting the file from the message should also delete the file from the folder on our disk, but sometimes it is not true. Removing the file from the directory, on the other hand, doesn't delete the copy of the file from the Outlook message.

What's more, processing a large number of files having the same name can lead to overstock. This will take place when the number of identically named file copies exceeds 99. Then, Outlook doesn't let us place a new identically named attachment in an e-mail.

The following procedure "Deleting_temporary_files" can be used to delete files from all  temporary folders used by Outlook.

Option Explicit
Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, _
ByVal pidl As ITEMIDLIST) As Long

Declare Function SHGetPathFromIDList Lib "Shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _
ByVal pszPath As String) As Long

Public Type SH_ITEMID
cb As Long
abID As Byte
End Type

Public Type ITEMIDLIST
mkid As SH_ITEMID
End Type

Public Const MAX_PATH As Integer = 260

Private Function fGetSpecialFolder(ByVal CSIDL As Long) As String
    Dim sPath As String
    Dim IDL As ITEMIDLIST
    fGetSpecialFolder = ""
    If SHGetSpecialFolderLocation(0, CSIDL, IDL) = 0 Then
        sPath = Space$(MAX_PATH)
        If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
            fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
        End If
    End If
End Function

Public Sub Deleting_temporary_files()
    Dim MyArray(300) As String, i&, file_name$
    file_name = Dir$(fGetSpecialFolder(32) & "Content.Outlook" & "\*.*", vbDirectory)
    i = 0
    Do While (Len(file_name) > 0) And (i < 101)
        MyArray(i) = file_name
        i = i + 1
        file_name = Dir$()
    Loop
    For i = 1 To UBound(MyArray)
        If Len(MyArray(i)) > 0 And MyArray(i) <> ".." Then
            On Error Resume Next
            Kill(CStr(fGetSpecialFolder(32) & "Content.Outlook\" & MyArray(i) & "\*.*"))
        End If
    Next i
End Sub  

In order to mount the procedure "Deleting_temporary_files" onto a button in MS Outlook menu, please refer to this article.


(c) Shon Oskar 

© All rights reserved. No part or whole of this article may not be reproduced or published without prior permission.
Original article publicated at this page


See Also