0
votes

EDIT: The code below works if anyone wants to use it. Just make sure to save your changes and restart outlook. Because that was my issue.

I've found many resources that explain how to trigger an event when a new email is received in outlook, but for whatever reason I can't get the initialization to work. Below is the the code I'm using. Debug.Print does not print and it doesn't seem like this runs at all. I'm getting no feedback from the system so I'm having trouble troubleshooting this. Any thoughts?

I've followed this YouTube video. Despite following the steps I'm not getting the same results. https://www.youtube.com/watch?v=5qcftHAWnZI

If it matters, I'm using Outlook 2013

Private WithEvents olItems As Outlook.Items

'On start up
Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")

Set olItems = olNS.GetDefaultFolder(olFolderInbox).Items

Debug.Print "Application_Startup trigerred " & Now()

End Sub

'New email recieved
Private Sub olItems_ItemAdd(ByVal item As Object)

Dim my_olMail As Outlook.MailItem
Dim olAtt As Outlook.Attachment

If TypeName(item) = "MailItem" Then
    Set my_olMail = item
        
        Debug.Print my_olMail.Subject
        Debug.Print my_olMail.Attachments.Count
        Debug.Print my_olMail.SenderEmailAddress
        
    Set my_olMail = Nothing
    
End If

End Sub
1

1 Answers

0
votes

I guess I'm dumb. All you need to do is save the changes and restart outlook again. smh