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

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
macdotcom Outlook 365 Outlook folder export to PST archival tool - close, but need a nudge Outlook VBA and Custom Forms 2
B How to get PST folder datetime? Outlook VBA and Custom Forms 1
M Search message, then (1) Jump to folder & (2) Select message that you searched for Outlook VBA and Custom Forms 6
G Search Folders and Jump to Folder Outlook VBA and Custom Forms 2
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2
P Yahoo/IMAP folder rename by Outlook desktop 365 Using Outlook 0
A Outlook 2019 folder counter Using Outlook 0
A Search folder and move the email Outlook VBA and Custom Forms 0
N Reply to Outlook messages by moving messages to a specific Outlook folder Outlook VBA and Custom Forms 1
T How to find or display the sub-folder name for an Archive Search Using Outlook 10
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
P Search folder: all emails sent to or from a domain Using Outlook 1
V Folder Properties - Gmail account can't switch Using Outlook 5
Victor_50 Outlook 2019 Jump to folder from search folder Outlook VBA and Custom Forms 0
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
I Help with Smart Folder + Query Builder on IMAP Using Outlook 0
S Paperclip icon shows without attachment in email under Sent folder Using Outlook 0
Kika Melo Outlook Calendar deleted appointments not in Deleted Items folder Using Outlook 3
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
Z Outlook 365 Automatically assign categories to incoming mail in a shared folder Round Robin Outlook VBA and Custom Forms 1
G Adding a contact to a specific folder Using Outlook 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
Rupert Dragwater Duplicate email in Folder Using Outlook 7
S Adding a recipient's column to Sent folder in Outlook 2010 Outlook VBA and Custom Forms 1
L "Insert Pictures" Button-Wrong Folder Using Outlook 5
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
CWM330 Outlook 365 Hey Diane! MS 365 Biz Standard and "Potential Spam" addressed to others coming to my JUNK folder? Using Outlook 2
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
A manual rule sends mail to wrong folder Using Outlook 5
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
D This folder up to date vs all folders up to date Using Outlook 1
G Automatically delete messages in the synchronization folder Outlook VBA and Custom Forms 3
wayneame Changing the Form Used by Existing Task Items in a Folder Outlook VBA and Custom Forms 4
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

Similar threads