简体   繁体   中英

VBA for Outlook - Change Subject Line using Right

I am trying to change incoming emails subject line to only the last 11 characters of the subject line. When I use Item.Subject = Right(Item.Subject,11) it does not work.

Can someone assist?

Full code.

Sub ChangeSubjectForward(Item As Outlook.MailItem)
Item.Subject = Right(Item.Subject, 11) 
Item.Save
End Sub

I found another SO thread that says you can't modify the subject of a message without opening it first. We can use ActiveInspector to get a handle on the Item after we display it. Then we can change it, save it, and close it. I added a check to see if the subject is actually longer than 11 characters before we attempt to truncate it.

Try this:

Public Sub ChangeSubjectForward(ByRef Item As Outlook.MailItem)
    Debug.Print Now                              '  This shows you when the code runs
    If Len(Item.Subject) > 11 Then
        Debug.Print "Subject is too long. Trimming..." '  This shows that we tried to truncate.

        Item.Display                             'Force the pop-up

        Dim thisInspector As Inspector
        Set thisInspector = Application.ActiveInspector

        Set Item = thisInspector.CurrentItem     ' Get the handle from the Inspector
        Item.Subject = Right$(Item.Subject, 11)
        Item.Save
        Item.Close
    End If
End Sub

You could create a macro rule then run the below code:

Sub save_to_dir_test1(mymail As MailItem)
Dim strID As String
Dim objMail As Outlook.MailItem

strID = mymail.EntryID
Set objMail = Application.Session.GetItemFromID(strID)
objMail.Subject = Right(m.Subject, 11)
objMail.Save
Set objMail = Nothing
End Sub

For more information, please refer to this link:

Run a Script Rule: Change Subject then Forward Message

Getting the incoming email in outlook via VBA

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