I have set a rule on outlook to delete permanently incoming email (based on subject condition).
I also set a vba macro to forward incoming emails (because any rules to forward emails are disabled by IT).
The problem: the macro of (forward incoming emails) is auto run before the outlook rule is executed.
I need to run my macro after that rule on outlook is executed and finished.
I do not prefer to transfer the rule itself into a vba code.
In advance, all thanks for your support.
Private Sub objInboxItems_ItemAdd(ByVal item As Object) 'Forward Incoming emails
Dim objMail As Outlook.MailItem
Dim objForward As Outlook.MailItem
If Not TypeOf item Is MailItem Then Exit Sub
Set objMail = item
Set objForward = objMail.Forward
With objForward
.Recipients.Add ("***@yahoo.com")
.Recipients.ResolveAll
.Send
End With
Set objMail = Nothing
Set objForward = Nothing
End Sub
CodePudding user response:
Force the rule to run on startup.
The assumption is this will run before ItemAdd code.
In ThisOutlookSession
Private Sub Application_Startup()
Dim st As Store
Dim myRules As Rules
Dim rl As Rule
Dim ruleName As String
Dim k As Long
' get store (can be any store)
Set st = Session.defaultStore
Debug.Print st.DisplayName
' get rules from specified store
Set myRules = st.GetRules()
ruleName = "name of rule to delete incoming email based on subject"
For k = 1 To myRules.count
Set rl = myRules(k)
Debug.Print myRules(k).name
If myRules(k).name = ruleName Then
' Regardless of rule store,
' the applicable Folder is by default the inbox of the default store
' Designate a folder if not the default inbox
rl.Execute ShowProgress:=True
Debug.Print ruleName & " has run."
Exit For
End If
Next
End Sub