compare folder contents to e-mail attachement

Not open for further replies.


Outlook version
Outlook 2010 64 bit
Email Account
Hi all,

i wanted to create a macro that compares the folder contents of a windows directory to the attachement file name of some selected e-mails ( of all at once ).

The attachements were saved to a windows folder using another script, replacing the attachment in the mail for a link to the saved file.

The goal of the macro I want is to delete attachements in the directory when the e-mail it used to be in was deleted... ( so that no excess attachements remain in the folder, wich don't have a mail anymore ).

I want to be able to choose the windows directory.

Can anyone help me with this?

I tried searching the net, but couldn't find such a code....

Thanks in advance
Outlook version
Email Account
Exchange Server

FileDialog will let you select a Windows directory/folder. FileSystem will let loop through all files in the folder. Scripting.FileSystem will let modify/delete the files - requires a reference to Microsoft Runtime Library.

The main procedure will have to copy the body of each email into a temp file and search for the link. Here's a sample which assumes all the emails are in a single Outlook Folder and have a single hyperlink which is the full path\name of the file. also the mails are small enough to search in one go rather than line by line search.

Sub Main()

Dim myNameSpace As Outlook.NameSpace

Dim myOLFolder As Folder

Dim myMailItem As mailItem

Dim strFolder, strFile, tempFile, strTempFile As String

Dim fso As New FileSystemObject

Dim fsoFile As Object

Set myNameSpace = Application.GetNamespace("MAPI")

Set myOLFolder = myNameSpace.PickFolder 'Pick Outlook Folder

tempFile = "C:\Temp\log.txt" 'Change to any convenient temp folder

strFolder = Select_Folder 'Select the Windows folder

strFile = Dir(strFolder & "\*") 'Loop through all files in the Windows folder

Do While Len(strFile) > 0

For Each myMailItem In myOLFolder 'Loop through emails in Outlook folder

'Save the email into a text file, and copy the text into a string

myMailItem.SaveAs tempFile

Set fsoFile = fso_OpenTextFile(tempFile, ForReading, True)

strTempFile = fsoFile.ReadAll

'search for the link, if found move to the next file

If InStrRev(strTempFile, strFile) = True Then GoTo Next_File


'If none of the emails found a match delete the file

fso.DeleteFile strFile


strFile = Dir


End Sub

Function Select_Folder() As String

With Application.FileDialog(msoFileDialogFolderPicker)

> AllowMultiSelect = False

> Title = "Select Data Folder"

> ButtonName = "Select"

> Show

On Error Resume Next

Select_Folder = .SelectedItems(1)

End With

End Function

Note that looping through all files in a folder and all mails in a Outlook folder and then performing string searches is bound to be inefficient.

Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J VBA for outlook to compare and sync between calendar Outlook VBA and Custom Forms 1
O Any decent tool to compare .pst files? Using Outlook 2
A Compare incoming FROM display name to contact Outlook VBA and Custom Forms 3
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 1
wayneame Changing the Form Used by Existing Task Items in a Folder Outlook VBA and Custom Forms 3
S Need code to allow defined starting folder and selection from there to drill down Outlook VBA and Custom Forms 10
P Emails assigned with a certain category (within a shared inbox) to be copied to a specific folder. Outlook VBA and Custom Forms 2
M Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker Outlook VBA and Custom Forms 6
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
S Folder Pane Colour Categories Using Outlook 6
Victor.Ayala Automated way to check the option "Show this folder as an email Address Book" Outlook VBA and Custom Forms 2
NVDon Create new Move To Folder list Outlook VBA and Custom Forms 0
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
O Cannot expand the folder. The set of folders cannot be opened. You do not have permission to log on. Using Outlook 1
F Jump to Inbox folder when click on Favorite Using Outlook 8
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
lcarpay Stay in the mail folder pane after ctrl-1 Using Outlook 1
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
bhamberg Shortcuts in Folder Pane (Outlook 2016) Using Outlook 19
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
P Posts in Folder No Longer Group by Conversation Column After Search Using Outlook 0
Y Filter unread emails in a search folder vba help Outlook VBA and Custom Forms 0
N contact list seen in Contact folder but knot in Address book or when 'TO' button is clicked in new email Using Outlook 0
D How to remove a folder, option grayed out Using Outlook 4
D How To Combine Share Task Folders in just one Folder Using Outlook 0
G Retention Policy for Deleted Items folder Using Outlook 0
Z Outlook 365 delete reminder you can’t make change to contents of this-read only folder Using Outlook 4
O The Outlook API wrongfully shows an outlook folder to have zero sub-folders Outlook VBA and Custom Forms 1
O The Outlook API wrongfully shows an outlook folder to have zero sub-folders Outlook VBA and Custom Forms 2
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
D Move Email with Attachment to Folder Outlook VBA and Custom Forms 3
C VBA to prompt for Sent folder destination Outlook VBA and Custom Forms 3
G How to add a folder shortcut to outlook quick access toolbar? Using Outlook 6
G Add to Outlook Contacts - Point to non-default contacts folder Using Outlook 0
M Move to Folder Using Outlook 1
K Export Folder to PST Outlook VBA and Custom Forms 2
B Spam folder not showing in Outlook Using Outlook 5
D Is a sub folder under contacts necessary to be able to name an Address Book? Using Outlook 1
J Moved many emails to Outlook external folder, need to delete on Gmail server Using Outlook 14
A Going to folder using shortcuts Using Outlook 3
B Outlook 2016 Auto-archive creates new folder Using Outlook 3
B BCC issues after changing root folder path for gmail Using Outlook 1
Fozzie Bear Outlook 2016 Creating a shared local Contacts folder Using Outlook 2
A Cannot copy this folder because it may contain private items Using Outlook 0
A Unable to save recurring Meeting to Documents folder due to error Using Outlook 2
B Extracting email addresses from a folder - how to also get the name of the person the address is for? Using Outlook 5
E How to display "Change Folder" in Change Default Email Delivery Location in Exchange Outlook 2016 Using Outlook 1
J Moving Imported folder Using Outlook 2
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1

Similar threads