How to automatically run a macro when opening a draft in Outlook? (add BCC)

I am trying to automatically execute this workflow:

  • when a user opens a draft message in Outlook ( generated eml file )
  • If the object matches the string (immutable, known in advance, I cannot change it, it's something like xyžřy, pay attention to characters other than ASCII):
  • then add the email to the BCC field (fixed, known in advance, valid email address, say baz@example.com)

I already know the last part - how to add BCC to the post , and I use InStr to match:

Sub addbcc()
Dim objRecip As Recipient
Set oMsg = Application.ActiveInspector.CurrentItem

With oMsg

     If InStr(1, oMsg.Subject, "xyžřy") > 0 Then

        Set objRecip = oMsg.Recipients.Add("baz@example.com")
        objRecip.Type = olBCC
        objRecip.Resolve

    End If

End With

Set oMsg = Nothing

End Sub

, - , , , , BCC . ?

+2
2

?

NewInspector, , .

Option Explicit
Private WithEvents Inspectors As Outlook.Inspectors

Private Sub Application_Startup()
    Initialize_handler
End Sub

Public Sub Initialize_handler()
    Set Inspectors = Application.Inspectors
End Sub

Private Sub Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
    If Inspector.currentItem.Class = olMail Then
        If Inspector.currentItem.Parent = "Drafts" Then ' Drafts Folder

            Debug.Print Inspector.currentItem.Subject ' Immediate Window
            ' Call Your Code
            ' Inspector.currentItem.BCC = "baz@example.com"
        End If
    End If
End Sub

CurrentItem

+2
+1

Source: https://habr.com/ru/post/1695323/


All Articles