How to run an Outlook script from a rule?
Hi all. I am working on a VBA script in Outlook 2019 that scans email objects and adds a text id to the subject line.
My intention is to run this script when an email is received. I had a prototype ready, but the Run a Script option in the Rules wizard is not there. Outlook has macros enabled, and I followed the instructions to alter the registry by adding a value to enable this option:
Computer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Security
REG_DWORD EnableUnsafeClientMailRules = 1
After doing this, I restarted Windows and opened Outlook. The option is not there.
While this might be a configuration issue, and a bit off topic, I wanted to post here because I want to be sure that I am on the right track as far how I am going about it.
My ultimate goal is to "catch" incoming emails, and if they are from a particular sender, then add the tag id to the subject line, move them to a designated folder, and lastly forward them to another email. At this time, I am catching the emails and forwarding them to the other email, but I have not figured out how to change the subject, except through VBA code as detailed below.
If the subject is "Apples are ready for harvest", and the email is from the defined sender then I want the subject to change to [Farming] Apples are ready for harvest", then have it forwarded to the email that will follow up.
Currently I have the following:
Sub AddTagId()
Dim myolApp As Outlook.Application
Dim aItem As Object
Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder
Dim iItemsUpdated As Integer
Dim strTemp As String
'Testing purposes...
iItemsUpdated = 0
For Each aItem In mail.Items
' aItem.Subject = "[Farming] " & aItem.Subject
iItemsUpdated = iItemsUpdated + 1
' aItem.Save
MsgBox "Total: " & mail.Items.Count & " " & aItem.Subject & " Sent date: " & Format$(aItem.SentOn(), "yyyy-mm-dd")
'Test code. I don't want this running forever if it finds a huge number
'of email items.
If iItemsUpdated > 10 Then Exit For
Next aItem
Set myolApp = Nothing
End Sub
Currently, it is not checking anything, it just blindly modifies the subject of all incoming email.
- The macro only processes items in the selected folder. How do I set this to the Inbox folder?
- Is there a way to limit my scan to just received emails, and not all emails in the Inbox? The MsgBox above displays Total: 3455. I really don't want to process all email, just what is being received.
- And in general, am I on the right path? Should this macro do more processing instead of using a rule. For example, I have set the rule to process only emails from a specific sender. Should I remove that rule and just check for the sender in the VBA code?
Of course, all of this is moot if I can't get the script to run. If I need to post this in another group (forum?) please et me know and I will do so. Thank you for your time and orientation. Saga