0

My code isn't working because emails are being forwarded to subfolders with a rule.

I cannot remove this rule nor make any rules on this computer.

I realized that newMailEx is probably the answer to my problems. Would there be a way to convert my code to run off of that instead of item_add?

Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.Folders.Item("Data").Folders.Item("Inbox").Items
End Sub
    
Private Sub Items_ItemAdd(ByVal Item As Object)
    Dim Msg As Outlook.MailItem
    If TypeOf Item Is Outlook.MailItem Then
        '''code to check subject lines and do various things to attachments for the various cases
    End If
End Sub
Community
  • 1
  • 1
Jason
  • 1
  • 1

1 Answers1

0

If you are creating a VBA macro in Outlook you need to choose the Application in the left drop-down menu and choose the NewMailEx event to generate the event handler in the editor.

NewMailEx event handler

After choosing the NewMailEx entry from the drop-down list you will get the following where you can add your code:

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

End Sub

So, in the event handler you may use the `` method in the following way:

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim mail as Outlook.MailItem
    Set mail = Application.Session.GetItemFromID(strEntryId)
    MsgBox mai.Subject
End Sub

Please remember that different kind of items can be passed to the event handler. For example, it can be an appointment item (meeting request). You need to check its message class before casting to the MailItem class to make sure you deal with mail items. Otherwise, an exception could be thrown if non-existing method or property is called.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks for the response! However, I'm still a little confused on how to integrate my current code into this function. For one, this is not my main inbox so I think I have to specify that somewhere. Additionally, does this function get incoming items passed into it? Because the parameter only lists entryIDCollection. I'm not sure how I would interact with the items through this function. Any other insights? – Jason Jun 10 '21 at 16:08
  • As shown in the sample code posted above, you can use the [GetItemFromID](https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.getitemfromid) method of the Namespace class to get the actual item (which is passed to the `ItemAdd` event in your case. – Eugene Astafiev Jun 10 '21 at 16:28