简体   繁体   中英

Updating email subject in Outlook VBA

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:

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

The IF statement is my attempt to cover the bases: user can either edit the email in the pop-up window, when the ActiveInpector is set or user can edit it in the reading pane - when the ActiveExplorer.Selection is set.

Problem is in the fact that while in the first case the macro works as expected, in the second the subject is not changed (even while I can see it changing while debugging the code). What is even more interesting if the message is selected but not edited (ie user hasn't clicked "reply" button) the macro works fine changing the topic in the message list.

Now, I've found this thread but a) it s over 6 years old and b) points to forum that doesn't exist anymore. As suggested in it, I've tried Item.Save method, but it doesn't seem to do anything except closing the edited message with original subject.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM