reubendayal
Senior Member
- Outlook version
- Outlook 365 64 bit
- Email Account
- Office 365 Exchange
Hi All,
I am trying to auto assign colour categories (which are based on team members in my team) on all Incoming emails to our shared mailbox. I've used the tips available on this slipstick thread - Processing Incoming E-mails with Macros but the code doesn't seem to fire as planned. I am going the VBA route as with rules the actions are a bit limited. Another challenge with rules is that I will need to write a script for the rule to fire and categorize the email based on who the sender is addressing it to.
One other issue I also see is that this code is made to work on a shared mailbox. But for some reason that could be creating a challenge. Perhaps my code which I have put in ThisOutlookSession, needs correction.
Here's what I have on the top of ThisOutlookSession:
Where mainly it is the 2nd line of Private "WithEvents Items As Outlook.Items" is what I am using as per the slipstick page's advise.
Further in the code for the application startup contains a few things as I am using it for other macros as well:
And then the ItemAdd event to do the things I am trying to achieve.
Any help is appreciated!
Thank you so much.
I am trying to auto assign colour categories (which are based on team members in my team) on all Incoming emails to our shared mailbox. I've used the tips available on this slipstick thread - Processing Incoming E-mails with Macros but the code doesn't seem to fire as planned. I am going the VBA route as with rules the actions are a bit limited. Another challenge with rules is that I will need to write a script for the rule to fire and categorize the email based on who the sender is addressing it to.
One other issue I also see is that this code is made to work on a shared mailbox. But for some reason that could be creating a challenge. Perhaps my code which I have put in ThisOutlookSession, needs correction.
Here's what I have on the top of ThisOutlookSession:
Code:
Option Explicit
Public RegularStartup As Boolean
Private WithEvents Items As Outlook.Items
Private WithEvents oExpl As Explorer
Private WithEvents oItem As MailItem
Private bDiscardEvents As Boolean
Private objNS As Outlook.NameSpace
Dim i As Long
Private WithEvents olInboxItems As Items
Dim oReply As MailItem
Dim MyCuFolder As Outlook.MAPIFolder
Where mainly it is the 2nd line of Private "WithEvents Items As Outlook.Items" is what I am using as per the slipstick page's advise.
Further in the code for the application startup contains a few things as I am using it for other macros as well:
Code:
Public Sub Application_Startup()
If RegularStartup = False Then
Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
Set oExpl = Application.ActiveExplorer
bDiscardEvents = False
'MsgBox "Done!"
ElseIf RegularStartup = True Then
Dim objMyInbox As Outlook.MAPIFolder
Set objNS = Application.GetNamespace("MAPI")
Set objMyInbox = objNS.GetDefaultFolder(olFolderInbox)
Set Items = objMyInbox.Items
Set objMyInbox = Nothing
Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
Set oExpl = Application.ActiveExplorer
bDiscardEvents = False
RegularStartup = False
MsgBox "Application Startup - Restarted!"
End If
End Sub
And then the ItemAdd event to do the things I am trying to achieve.
Code:
Private Sub Items_ItemAdd(ByVal Item As Object)
'Ensure we are only working with e-mail items
Dim EmlItem As MailItem
Dim EmlBody As String
Dim EmlSubj As String
'Dim MyFolder As Folder
If Item.Class <> OlMail Then Exit Sub
Set EmlItem = Item
MsgBox EmlItem.Subject
If LCase(InStr(EmlItem.Subject, "re:")) Or _
LCase(InStr(EmlItem.Subject, "fw:")) Then
Exit Sub
EmlBody = EmlItem.Body
'If InStr(EmlSubj, "Welcome to Deloitte" & vbCrLf & vbCrLf & "Maersk Immigration Services") Then
If LCase(InStr(EmlBody, "dear anne")) Or _
LCase(InStr(EmlBody, "dear anna")) Or _
LCase(InStr(EmlBody, "hi anne")) Or _
LCase(InStr(EmlBody, "hi anna")) Then _
Item.Categories = "Anne"
ElseIf LCase(InStr(EmlBody, "dear reuben")) _
Or LCase(InStr(EmlBody, "dear rueben")) _
Or LCase(InStr(EmlBody, "hi reuben")) _
Or LCase(InStr(EmlBody, "hi reuben")) _
Or LCase(InStr(EmlBody, "dear ruben")) _
Or LCase(InStr(EmlBody, "hi ruben")) _
Then Item.Categories = "Reuben"
End If
Set EmlItem = Nothing
End Sub
Any help is appreciated!
Thank you so much.