0

I've written something that quickly changes an email subject to add the sender initials, so that I can click it before I add it to a JIRA issue using the Outlook JIRA plugin.

Currently, this is mildly annoying because it has to open the message to change the subject of the email and then when it returns, it moves onto the next message. This adds the mild inconvenience of having to make me move back to the correct message.

I'd be grateful for help figuring out how to make it return to the correct message. Maybe with MailItem.EntryID but I can't figure out how to point to it on close.

Sub EditSubject()

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)
    Item.Display   'Force the pop-up
    Set oInspector = Application.ActiveInspector  'Reassign oInpsector and Item again
    Set Item = oInspector.CurrentItem
Else
   Set Item = oInspector.CurrentItem
End If

Dim Initials As String
strSubject = Item.Subject

Dim splitName() As String
splitName = Split(Item.SenderName, ",")

Initials = Left$(splitName(1), 2)
Initials = Right$(Initials, 1) + Left$(splitName(0), 1)

Item.Subject = UCase$(Initials) & " - " & strSubject

Item.Close (olSave)

Set Item = Nothing
Set oInspector = Nothing

End Sub

References: [1] Saving emails with sender's initials

[2] Updating email subject in Outlook VBA

messgie
  • 3
  • 5

2 Answers2

0

There is no need to open an inspector window for an item. You can change the Subject line without opening an actual item in a new inspector window.

Sub EditSubject()

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

Dim Initials As String
strSubject = Item.Subject

Dim splitName() As String
splitName = Split(Item.SenderName, ",")

Initials = Left$(splitName(1), 2)
Initials = Right$(Initials, 1) + Left$(splitName(0), 1)

Item.Subject = UCase$(Initials) & " - " & strSubject

Item.Save

Set Item = Nothing
Set oInspector = Nothing

End Sub
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • That doesn't work for me; if I don't open the message it won't change the subject at all. I'm using MS Office Professional Plus 2016, if that makes a difference. – messgie May 22 '20 at 16:26
  • Outlook likes to cache old values until the view is changed. After you update all subject properties you change folders to any folder and then return it back. This operation will allow refreshing the view and update subject lines according to the ones set programmatically. – Eugene Astafiev May 22 '20 at 16:38
  • Have you tried it though? Because it really doesn't let you change the subject from the reading pane. https://support.office.com/en-gb/article/edit-an-email-subject-line-08d60328-2a27-473e-8227-0b42a1ff2ce6 – messgie May 22 '20 at 17:09
0

The problem was not reproduced but you can reselect the item.

Option Explicit

Sub EditSubject_PreserveSelection()

Dim Item As mailItem

If ActiveInspector Is Nothing Then
    Set Item = ActiveExplorer.Selection.Item(1)
    Item.Display   'Force the pop-up
Else
   Set Item = ActiveInspector.currentItem
End If

Dim Initials As String
Dim splitName() As String
'splitName = Split(Item.SenderName, ",")

'Initials = Left$(splitName(1), 2)
'Initials = Right$(Initials, 1) + Left$(splitName(0), 1)

'Item.subject = UCase$(Initials) & " - " & strSubject
Item.subject = "Initials" & " - " & Item.subject
Item.Close (olSave)

ActiveExplorer.ClearSelection
ActiveExplorer.AddToSelection Item

Set Item = Nothing

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52