如何在Outlook中打开草稿时自动运行宏? (添加密件抄送) [英] How to automatically run a macro on opening a draft in Outlook? (add a BCC)

查看:165
本文介绍了如何在Outlook中打开草稿时自动运行宏? (添加密件抄送)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我已经知道最后一部分-如何添加密件抄送给邮件,并且我使用InStr进行匹配:

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

但是,用户仍然需要记住按一个按钮来运行此宏,这并不比手动键入BCC更方便.打开此电子邮件后,是否可以自动运行宏 ?

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?

推荐答案

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

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.

示例

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属性

这篇关于如何在Outlook中打开草稿时自动运行宏? (添加密件抄送)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆