在Outlook VBA中更新电子邮件主题

时间:2016-08-24 23:07:39

标签: vba outlook outlook-vba

我正在尝试创建一个可以更改电子邮件主题的按钮控制的宏。在this thread之后,我设法提出了这个问题:

Public Sub Confidential()

Dim Item As Outlook.MailItem
Dim oInspector As Inspector
Dim strSubject As String

Set oInspector = Application.ActiveInspector
If oInspector Is Nothing Then
    Set Item = Application.ActiveExplorer.Selection.Item(1)
Else
   Set Item = oInspector.CurrentItem
End If

strSubject = Item.Subject

' Remove previous Confidential and Legally Privileged
strSubject = Replace(strSubject, "Confidential and Legally Privileged ", "")

' Prefix subject with Confidential and Legally Privileged
strSubject = "Confidential and Legally Privileged " & strSubject

' Set the message subject
Item.Subject = strSubject


Set Item = Nothing
Set oInspector = Nothing


End Sub

IF语句是我尝试覆盖基础:用户可以在弹出窗口中编辑电子邮件,设置ActiveInpector或用户可以在reading pane中编辑它 - 当ActiveExplorer.Selection已经确定了。

问题在于,在第一种情况下,宏按预期工作,在第二种情况下,主题不会更改(即使我在调试代码时可以看到它发生变化)。如果选择了邮件但没有编辑(即用户没有点击"回复"按钮),那么更有趣的是宏可以正常更改邮件列表中的主题。

现在,我发现this thread但是a)它超过6岁而且b)指向不再存在的论坛。正如其中所建议的那样,我尝试了Item.Save方法,但除了用原始主题关闭已编辑的邮件之外,它似乎没有做任何事情。

2 个答案:

答案 0 :(得分:3)

感谢@Ryan Wildry:如果在资源管理器窗格中编辑了电子邮件,请使用.Display方法强制弹出窗口,然后使用它:

Dim Item As Outlook.MailItem
Dim oInspector As Inspector
Set oInspector = Application.ActiveInspector

If oInspector Is Nothing Then
    Set Item = Application.ActiveExplorer.Selection.Item(1)
    Item.Display   'Force the po-up
    Set oInspector = Application.ActiveInspector  'Reassign oInpsector and Item again
    Set Item = oInspector.CurrentItem
Else
   Set Item = oInspector.CurrentItem
End If

答案 1 :(得分:0)

这是你想要做的吗?

Option Explicit
Public Sub Confidential()
    Dim Item As Outlook.MailItem
    Dim oInspector As Inspector
    Dim strSubject As String
    Dim strPrefixSubject As String

    Set oInspector = Application.ActiveInspector

    If oInspector Is Nothing Then
        Set Item = Application.ActiveExplorer.Selection.Item(1)
    Else
       Set Item = oInspector.CurrentItem
    End If

    strSubject = "Confidential and Legally Privileged "

    Debug.Print Item.Subject

    ' Remove previous Confidential and Legally Privileged
    Item.Subject = Replace(Item.Subject, strSubject, "", vbTextCompare)
    Item.Save

    ' Prefix subject with Confidential and Legally Privileged
    strPrefixSubject = "Confidential and Legally Privileged " & Item.Subject

    ' Set the message subject
    Item.Subject = strPrefixSubject
    Item.Save


    Set Item = Nothing
    Set oInspector = Nothing

End Sub