VBA Moving Email Based on Attachment Filename in Outlook

Raychin

New Member
Joined
Apr 7, 2022
Messages
25
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I need to move specific emails containing specific filename in .csv format. I have a macro but can't get it work. I am new to Outlook VBA so any help will be appreciated.
My folders are like this : Outlook Inbox-->Mo SA Power Forecast - this is the folder the mails are arriving by Rule; and from this folder (Mo SA Power Forecast) i want to move a mail with attachment name "******-wind-power-forecast-HrabrovoWind.csv" to another sub Inbox folder called "Meteologica Hrabrovo Forecast". Basically i need the mail, arriving in Inbox/Mo SA Power Forecast, containing attachment "******-wind-power-forecast-HrabrovoWind.csv", to be moved to folder Meteologica Hrabrovo Forecast (Inbox/Meteologica Hrabrovo Forecast).
Can you help me, please?

P.P.

The reason it can't be done with simple Outlook Rules is that there is 2 mails with the same name in the Subject, arriving in the "Mo SA Power Forecast" folder, but with different attachments in them!

This is the code i have :


Public WithEvents objMails As Outlook.Items

Private Sub Application_Startup()
Set objMails = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objMails_ItemAdd(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objAttachment As Outlook.Attachment
Dim strAttachmentName As String
Dim objInboxFolder As Outlook.Folder
Dim objTargetFolder As Outlook.Folder

'MSGbox just to se wehn scrip runs.
MsgBox "Script running"


'Ensure the incoming item is an email

If TypeOf Item Is MailItem Then
Set objMail = Item
Set objAttachments = objMail.Attachments

'Check if the incoming email contains one or more attachments

If objAttachments.Count > 0 Then
For Each objAttachment In objAttachments
strAttachmentName = objAttachment.DisplayName
Set objInboxFolder = Application.Session.GetDefaultFolder(olFolderInbox)

If InStr(LCase(strAttachmentName), "-wind-power-forecast-HrabrovoWind.csv") > 0 Then
Set objTargetFolder = objInboxFolder.Parent.Folders("Inbox").Folders("Meteologica Hrabrovo Forecast")
End If
Next

'Move the email to specific folder

objMail.Move objTargetFolder
End If
End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top