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
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