Rick
Rick

Reputation: 11

Change outlook subject line

How do I write a VBA method that replaces the subject if there is a specific word in the subject. This code would find a certain key word (different than the subject key word) in the body of the email. It would then replace the subject line with 13 charactars after the key word found in the body of the text.

The below was already found but doesn't mention how to find anything in the body of the email. And I don't get the MAPI reference.

Any help would truly be appreciated Thank You in advance for any assistance Rick

Sub RewriteSubject(MyMail As MailItem)
  Dim mailId As String
  Dim outlookNS As Outlook.NameSpace
  Dim myMailItem As Outlook.MailItem

  mailId = MyMail.EntryID

  Set outlookNS = Application.GetNamespace("MAPI")
  Set myMailItem = outlookNS .GetItemFromID(mailId)

  ' Do any detection here
  mailItem.Subject = "Dept - " & mailItem.Subject
  myMailItem.Save

  Set mailItem = Nothing
  Set outlookNS = Nothing
End Sub 

Upvotes: 1

Views: 3360

Answers (1)

Shoban
Shoban

Reputation: 23016

If it is for all new messages then you can use the following

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim v As Variant
    For Each v In Array("first", "second")
        If InStr(1, Item.Subject, v, vbTextCompare) <> 0 Then
            SearchForAttachWords = True
        End If
    Next

    If SearchForAttachWords = True Then
        Item.Subject = "Whatever subject you want"
    End If      

End Sub

Hope this helps.

Upvotes: 1

Related Questions