如何在Outlook中打开草稿时自动运行宏? (添加BCC)

时间:2016-06-02 12:44:51

标签: vba outlook outlook-vba

我尝试自动实现此工作流程:

  • 用户在Outlook中打开邮件草稿时(generated EML file
  • 如果主题与字符串匹配(不可变,事先已知,我无法更改;它类似于baz@example.com,请注意非ASCII字符):
  • 然后向BCC字段添加电子邮件(不可变,事先知道,有效的电子邮件地址;让我们说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

我已经知道最后一部分 - how to add a BCC to a message,我使用InStr进行匹配:

{{1}}

但是,用户仍然需要记住按下按钮才能运行此宏,这比手动输入BCC更方便。打开此电子邮件时是否可以自动运行宏

2 个答案:

答案 0 :(得分:2)

  

打开此电子邮件时是否可以自动运行宏?

使用 NewInspector Event ,当用户或代码打开新窗口时会发生事件。

实施例

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

答案 1 :(得分:1)

您可以使用ItemAdd监控草稿文件夹。在此处查看收件箱的想法。 How do I trigger a macro to run after a new mail is received in Outlook?

您可以在ItemSend中添加密送。 Outlook 2010 - VBA - Set bcc in ItemSend