2

I'm trying to automatically achieve this workflow:

  • when user opens a message draft in Outlook (a generated EML file)
  • if the subject matches a string (immutable, known beforehand, I can't change it; it's something like xyžřy, note the non-ASCII characters):
  • then add an e-mail to BCC field (immutable, known beforehand, valid e-mail address; let's say it's baz@example.com)

I already know the last part - how to add a BCC to a message, and I use InStr for matching:

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

However, the user still needs to remember to press a button to run this macro, which is not more convenient than typing the BCC manually. Is it possible to run the macro automatically when this e-mail is opened?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • I'm aware that this looks like an XY Problem ("well add the BCC to the generated draft, duh"). Alas, it seems that loading BCC from a draft EML message (my end goal) is something Outlook cannot do, so I'm looking for ork-arounds. There are add-ins for this, but I'm briefly exploring other alternatives that don't involve complex third-party code for such a tiny issue; I'm okay with eventually using such an add-in if no other options are available; but in a largish deployment, this wouldn't be "just fork over 20 bucks, add an auto-BCC rule and be done with it" (else I wouldn't even bother). – Piskvor left the building Jun 02 '16 at 12:57
  • Are any of the examples on this page helpful: https://msdn.microsoft.com/en-us/library/office/ff865989.aspx – Jordan Jun 02 '16 at 14:01

2 Answers2

2

Is it possible to run the macro automatically when this e-mail is opened?

Work with NewInspector Event , Events occurs when new window is opened by user or through your code.

Example

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 Property

0m3r
  • 12,286
  • 15
  • 35
  • 71
1

You could monitor the drafts folder with ItemAdd. See the idea here for the inbox. How do I trigger a macro to run after a new mail is received in Outlook?

You could add the bcc in ItemSend. Outlook 2010 - VBA - Set bcc in ItemSend

Community
  • 1
  • 1
niton
  • 8,771
  • 21
  • 32
  • 52