0

I need to organise my mailbox in order not to lost a specific email when deduplicate the Inbox sub folder. The issue i have is that i receive every single day at specific time two emails in my Inbox with the same name, but with different files in them as an attachments. The emails are moved from Inbox to a sub folder by rule. I need one of the emails to be moved to a concrete folder, but since the mails have the same subject name and body, i can't use a rule to do that. So i have a macro that i assume, have to do the job but it doesn't. Also since the mails arrived every morning, i can't check my macro works, and i dont know how to modify the code to check the entire subfolder and executes the task for emails already inside. The folder structure i have : Inbox ---> Receiving folder name: "Meteologica SA Power Forecast"---> Target folder name: "Meteologica Hrabrovo Forecast". Basically i need an email, allways have file attachment in it with name having this part "-wind-power-forecast-HrabrovoWind(.csv)" to be moved from "Meteologica SA Power Forecast" sub folder, to the "Meteologica Hrabrovo Forecast" target sub folder of my Outlook Inbox. I just don't have the knowledge to make it works...Can you help me with that, please? Here's the code i have but i don't know what's wrong:

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

   Sub Copy_Hrabrovo()

   Dim ns As NameSpace
   Dim olInboxFolder As MAPIFolder, olSubFolder As MAPIFolder
   Dim msg As MailItem
   Dim objAttachments As Outlook.Attachments
   Dim objAttachment As Outlook.Attachment
   Dim strAttachmentName As String

   Set ns = GetNamespace("MAPI")
   Set olInboxFolder = ns.GetDefaultFolder(olFolderInbox)
   Set olSubFolder = olInboxFolder.Folders("Meteologica SA Power Forecast")
   If TypeOf Item Is MailItem Then
    Set objMail = Item
    Set objAttachments = objMail.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") > 0 Then
                    Set objTargetFolder = objInboxFolder.Folders("Meteologica Hrabrovo Forecast")
                 
                    End If
                Next
                obfMail.Move objTargetFolder
       End If        
   End If
  
  Set olSubFolder = Nothing
  Set olInboxFolder = Nothing
  Set ns = Nothing

  End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Harati
  • 1
  • 3

1 Answers1

0

In the code the item object is not declared:

 If TypeOf Item Is MailItem Then
    Set objMail = Item

It seems you have copied that piece from any other event handler where the Item instance was passed as a parameter. If you need to get the currently selected item in Outlook you need to use the Selection property of the Explorer class which returns a Selection object that contains the item or items that are selected in the explorer window. For example:

Dim myOlExp as Outlook.Explorer
Dim myOlSel as Outlook.Selection
Set myOlExp = Application.ActiveExplorer 
Set myOlSel = myOlExp.Selection 
For x = 1 To myOlSel.Count 
 If myOlSel.Item(x).Class = OlObjectClass.olMail Then 
 
 Set oMail = myOlSel.Item(x) 
End If

But if you want to assign your VBA macro to the existing rule in Outlook you need to change the signature of the method in the following way so it will accept an instance of the MailItem class:

Sub Copy_Hrabrovo(Item as MailItem)

Also you may consider handling the ItemAdd event on the folder where items are moved and require any further processing by your VBA sub.

You need to define the collection with the WithEvents keyword to handle the ItemAdd event:

Public WithEvents myOlItems As Outlook.Items 

Public Sub Initialize_handler() 
  Set myOlItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items 
End Sub 
 
Private Sub myOlItems_ItemAdd(ByVal Item As Object) 
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Well i don't need to assign the code to the rule that puts this emails in the source sub folder for this macro. The goal is the macro to runs constantly and when this two emails arrive in t he source sub folder to kicks in and tho move one of those mails to the target sub folder. So basically i need to declare the object in the sub folder that has to be moved and who is it that object? – Harati Nov 15 '22 at 13:59
  • It was not clear where and when you are going to run the sub at all. Anyway, you can handle the `ItemAdd` event of the `Items` class which comes from the folder where you need to monitor for new items. – Eugene Astafiev Nov 15 '22 at 14:20
  • Yes, i didn't mention where and how i want to use the macro. So with ItemAdd i will "watch"a folder and what happens in it. I assume i have to add some lines in Start Up macro as well? – Harati Nov 15 '22 at 15:26
  • The event is fired when an item is added to the folder - moved, arrived and etc. You need to define the `objMails` with the `WithEvents` keyword. – Eugene Astafiev Nov 15 '22 at 15:28
  • So it has to be something like this : 'Private WithEvents objNewMailItems As Outlook.Items Private Sub Application_Startup() Set objMyInbox = objNS.GetDefaultFolder(olFolderMeteologicaSAPowerForecast) Set olInboxItems = Session.GetDefaultFolder(olFolderMeteologicaSAPowerForecast).Items Set objNewMailItems = objMyInbox.Items End Sub Private Sub objNewMailItems_ItemAdd(ByVal Item As Object) End Sub' – Harati Nov 16 '22 at 10:31
  • Yes, you are on the right avenue. – Eugene Astafiev Nov 16 '22 at 10:33
  • Aha, but where i have to insert the code that will "watch" the sub folder from which i want to move the mail i need? Under the "Private Sub objNewMailItems_ItemAdd(ByVal Item As Object)" line? – Harati Nov 16 '22 at 10:35
  • In that case you can handle the [Folder.BeforeItemMove](https://learn.microsoft.com/en-us/office/vba/api/outlook.folder.beforeitemmove) event. – Eugene Astafiev Nov 16 '22 at 10:42
  • Well i'm stuck now.... i am not familiar with vba enough to modify this for my needs and to continue forward... :/ Can you Help me with the correct look for the code, please? – Harati Nov 16 '22 at 12:11