Rename Subject Line with Email Content

Status
Not open for further replies.

Not Mr. Robot

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Hello, I am farily new to VBA and I was wondering if it is possible to rename subject lines in Outlook with certain words and numbers derived from the body of an email and/or an attachement (PDF or Word). I created script that renames the subject lines based on domains/original subject lines, but I really need more information from within the emails without having to open each one up.

Thanks!
John
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange

Not Mr. Robot

Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Thanks, Diane! I've been playing around with one of the examples, and it is close to what I am looking for. However, I am running into a couple of hiccups:
  • The original subject line does not get deleted
  • The script won't run on all items in my box when I ask it to do so (it seems to work on one email and then just stops)
Here is what I have so far:

Code:
Sub VendorSubjectRegExTest(item As Outlook.MailItem)

    Dim olMail As Outlook.MailItem
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match
    Dim strSubject As String
    Dim testSubject As String
       
    Set olMail = Application.ActiveExplorer().Selection(1)
   
    Set Reg1 = New RegExp
  
For i = 1 To 3

With Reg1
    Select Case i
    Case 1
        .Pattern = "(SELLER:\s([\w-\s]*)\s*)\n [^ECORP]"
        .Global = False
      
    Case 2
        .Pattern = "(BUYER:\s([\w-\s]*)\s*)\n [^ECORP]"
        .Global = False
     
    Case 3
        .Pattern = "(PRODUCT:\s([\w-\s]*)\s*)\n"
        .Global = False
                  
    End Select
  
End With
  
        If Reg1.Test(olMail.Body) Then
   
        Set M1 = Reg1.Execute(olMail.Body)
        For Each M In M1
            Debug.Print M.SubMatches(1)
            strSubject = M.SubMatches(1)
          
         strSubject = Replace(strSubject, Chr(13), "")
         testSubject = testSubject & " " & Trim(strSubject) & " Vendor"
         Debug.Print i & testSubject
       
         Next
    End If
        
Next i

Debug.Print olMail.Subject & testSubject
olMail.Subject = olMail.Subject & testSubject
olMail.Save

Set Reg1 = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
>> Set olMail = Application.ActiveExplorer().Selection(1)
this tells it to run on the selected message. this is supposed to be a rule, so you'll need to remove that line and change all instances of olMail to Item

>> olMail.Subject = olMail.Subject & testSubject
this sets the subject to be the subject + the text from the regex pattern

if you want just the regex pattern result, use
olMail.Subject = testSubject
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
C How to rename subject line and forward the email Outlook VBA and Custom Forms 2
C Save outlook attachments and rename/append files with identifier from subject line Outlook VBA and Custom Forms 3
D VBA script to auto download attachments and rename file according to subject line Outlook VBA and Custom Forms 23
W Save and rename outlook email attachments to include domain name & date received Outlook VBA and Custom Forms 4
W Save Outlook attachment in network folder and rename to current date and time Outlook VBA and Custom Forms 18
D Autosave Attachment and Rename Outlook VBA and Custom Forms 1
F Rename Contacts Greyed Out Using Outlook 0
K IMAP Server Wants to alert you to the following: cannpt rename system folder Using Outlook 1
Diane Poremsky Rename Email Accounts and Data Files in the Folder List New Slipstick.com Articles 0
S VBA code to rename a task (flagged message) Outlook VBA and Custom Forms 1
B Your IMAP server wants to alert you to the following: cannot rename system folders Using Outlook 8
L IMAP server wants to alert you to the following: cannot rename sytem folders Using Outlook 14
T Rename Outlook 2010 .PST Files Using Outlook 14
B IMAP server wants to alert you to the following cannot rename system folders Using Outlook 3
M Outlook Today. How to rename IMAP inbox accounts to something else than inbox? Using Outlook 4
A Rename resource Exchange 2003 Using Outlook 2
O Run Rule to Rename Email and Add to Tasks Using Outlook 4
U How do rename a recovered folder after repairing using scanpst.exe Using Outlook 1
M Rename CCR 2007 Cluster Exchange Server Administration 1
1 'Sent Items', 'Deleted Items', & 'Junk Items' Folders; Want to Rename Them to Just: 'Sent', 'Deleted Using Outlook 1
R Rename Outlook .pst folders Using Outlook 1
F Rename option greyed out when trying to rename folders under favorite folders Using Outlook 2
T Rename Archive Mailbox Exchange Server Administration 5
S Rename NETBIOS name in windows 2003 AD with Exchange 2003 Exchange Server Administration 6
D How to rename the display name of the primary outlook mailbox in Outlook 2010 / Exchange 2007 Using Outlook 1
T Rename CAS array Exchange Server Administration 4
K Rename Server with Exchange 2010 Exchange Server Administration 2
W Outlook folder rename (right click 'rename' option) issue Using Outlook 9
S Is there a configuration or setting in Outlook (2007 or 2010) that allows users to rename "shared" c Using Outlook 3
O How to rename folder name within favorities under Outlook? Using Outlook 3
A Cant rename Mailbox Database? Cant Create new Database Exchange Server Administration 6
M Rename Database BCM (Business Contact Manager) 1
J how do i rename the default fields in MS Business Contact manager BCM (Business Contact Manager) 1
D Command line to Copy or Rename Mail Profile Using Outlook 1
M Outlook 2013 Script Assistance - Save Opened Link with Subject Added Outlook VBA and Custom Forms 1
B Add Prefix text to Subject Line Using Outlook 1
P [SOLVED] Auto remove [EXTERNAL] from subject Using Outlook 6
M Outlook 2013 Replace Subject with Conversation (a "hidden" value). Outlook VBA and Custom Forms 0
M Convert Subject Line to Internet Header version of Subject Outlook VBA and Custom Forms 10
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
P Auto Insert Current Date or Time into Email Subject Outlook VBA and Custom Forms 2
M VBA to auto forward message with new subject and body text Outlook VBA and Custom Forms 8
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
B Remove Subject Residual Outlook VBA and Custom Forms 3
F VBA to ensure a code is entered in Subject title Outlook VBA and Custom Forms 1
A Check for words in subject header before sending email Outlook VBA and Custom Forms 4
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
Z Outlook Custom Form: Adding Dropdown(Project Code) at the end of subject Outlook VBA and Custom Forms 0
D Remove text in subject using VBA Outlook VBA and Custom Forms 4
Similar threads


















































Top