compare folder contents to e-mail attachement

Status
Not open for further replies.

RSM87

Member
Outlook version
Outlook 2010 64 bit
Email Account
IMAP
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
Outlook 2007
Email Account
Exchange Server
Hi,

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

Next

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

fso.DeleteFile strFile

Next_File:

strFile = Dir

Loop

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.

Regards,
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
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
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
Horsepower Moving emails between folder Outlook for Mac Outlook Wishlist 8
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 Outlook.com 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
J Sent Items Folder NOT Showing Correct From Email Address Using Outlook 0
B VBA to Collapse Task Folder Groups Outlook VBA and Custom Forms 1
Retired Geek Junk Folder Clean Up Rules Exchange Server Administration 1
D Outlook 2016 Deleting emails without affecting folder structure - Outlook Office 365 Using Outlook 3
B Outlook 365 - Folder pane list clears Using Outlook 28
P Add, remove, & reorder folder pane Using Outlook 6
R Folder pane width change Using Outlook 62
T Missing Folder in Outlook.com Using Outlook 3
R Setup autofoward rule on a particular folder in Outlook Using Outlook 0
T After I send a new email, it remains in the Draft folder Using Outlook.com accounts in Outlook 3
N Outlook 2016 Folder Icons Look Using Outlook 0
S Create A Search Folder That Looks For Message Class? Outlook VBA and Custom Forms 0
R Deleting emails from storage folder Outlook VBA and Custom Forms 1
B Outlook - Mail from safe senders list being sent to Spam Folder Using Outlook 0
R Search/Jump to a folder by typing its name Outlook VBA and Custom Forms 1
M Sent mail not showing in Sent Items folder; but they can be found with Search Using Outlook 3
A Create date folder and move messages daily Outlook VBA and Custom Forms 1
9 Outlook 2016 How to save an Outlook attachment to a specific folder then delete the email it came from? Using Outlook 1

Similar threads

Top