首页 文章

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

提问于
浏览
2

我正在尝试创建一个按钮控制的宏,它将改变电子邮件的主题 . 在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 回答

  • 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
    
  • 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
    

相关问题