Updating email subject in Outlook VBA

前端 未结 2 1404
感情败类
感情败类 2021-01-13 23:13

I am trying to create a button-controlled macro that would change the topic of an email message. Following this thread I\'ve managed to came up with this:

Pu         


        
相关标签:
2条回答
  • 2021-01-13 23:45

    Is this what your trying to do?

    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
    
    0 讨论(0)
  • 2021-01-13 23:56

    Answered thanks to @Ryan Wildry: if the email is edited in the explorer pane, use the .Display method to force a pop-up and then work with it:

    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 讨论(0)
提交回复
热议问题