Hello,
I'm a VBA novice using Windows Outlook 2016 and want to set a rule for all incoming messages that runs a script categorizing the mail as internal or external. The standard rules feature can almost cover this function, except for matters related to CC. For example, when a colleague sends an email to a client with me on CC (external) or when a colleague sends an email to another colleague with me on CC (internal), I haven't found a way to differentiate this with standard rules.
So, I looked into the matter and tried to write something. I want to compare the number of @s and the number of "mycompany.com" to determine if an external email address is present. But I'm not sure it's formatted correctly to be a script...I also get this error regarding the If InStr line:
Run-time error '91:
Object variable or With block variable not set
Public Sub InternalMail()
Dim olMail As MailItem
Dim myDomain As String
myDomain = "@mycompany.com"
With olMail
If InStr(1, .SenderEmailAddress, myDomain) > 0 Then 'Sender is internal
'Check for externals in CC list
If Not .CC = vbNullString Then 'There is a CC entry
If UBound(Split(.CC, "@")) = UBound(Split(.CC, myDomain)) Then
olMail.Categories = "Internal"
Else 'the CC is external
olMail.Categories = "External"
End If
End If
'Check for externals in Recipient list
If UBound(Split(.To, "@")) = UBound(Split(.To, myDomain)) Then
olMail.Categories = "Internal"
Else 'the CC is external
olMail.Categories = "External"
End If
Else 'Sender is external
olMail.Categories = "External"
End If
End With
End Sub
Can someone take a look at what I got and offer guidance? I'd greatly appreciate it.
-CS