question

CS11-4219 avatar image
0 Votes"
CS11-4219 asked CS11-4219 commented

I'd like to run a rule script on Outlook 2016...

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

office-vba-dev
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @CS11-4219 ,
Thanks for your reply.
Please kindly understand the tag” office-outlook-itpro” which mainly focus on general issues about Outlook desktop client, considering that you issue is more related to VBA. In order to better resolve your problem, I will add the tag” office-vba-dev”.

Thanks for your understanding and hope your issue would be resolved soon.

0 Votes 0 ·

Hi Faery. Sorry about that, Re-tagged just now.

0 Votes 0 ·

0 Answers