【问题标题】:Updating email subject in Outlook VBA在 Outlook VBA 中更新电子邮件主题
【发布时间】:2016-08-24 23:07:39
【问题描述】:

我正在尝试创建一个按钮控制的宏来更改电子邮件的主题。在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 方法,但它似乎没有做任何事情,除了关闭带有原始主题的编辑消息。

【问题讨论】:

  • 它会抛出任何错误吗?
  • 不 - 执行没有问题,我可以看到 item.subject 对象的变化,但它没有显示在屏幕上。
  • 您在更改之前还是之后致电.Display?我在上面的代码中没有看到。
  • 所以您尝试将主题从 Confidential and Legally Privileged 更改为 Confidential and Legally Privileged Confidential and Legally Privileged
  • @Om3r 不,有一个replace 方法可以删除已经存在的“机密....”文本:strSubject = Replace(strSubject, "Confidential and Legally Privileged ", "")

标签: vba outlook


【解决方案1】:

感谢@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

【讨论】:

    【解决方案2】:

    这是你想要做的吗?

    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
    

    【讨论】:

    • 不是真的 - 这就是我最初遇到问题的地方:此解决方案关闭窗口(因此用户必须再次单击“回复”)并且它不会更新消息列表中的主题 - 如果原来的主题是“测试”,点击消息列表中的按钮后,它将保持为“测试”,但确实点击回复(第二次 - 不太友好)将显示更改的主题:s3.postimg.org/8d6wj7k7n/outlook.png
    猜你喜欢
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2014-05-21
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    相关资源
    最近更新 更多