Consulting

Results 1 to 4 of 4

Thread: Script to change subjects

  1. #1

    Script to change subjects

    Having a problem with VBA scripting in Outlook. I have these emails that come in and they all have a generic subject header followed by a specific external part number. I want to run the script so it strips out all the junk and replaces the entire subject with just our internal part number. This script should examine the email subject, and if the subject contains XXXXXX001, then replaces the entire subject with YYYYYY001. Here are some examples of the input and the result:


    Incoming Subject: "Automated XYZ Company Order: Part Description (02246744)"
    Resulting Subject: "YYYYYY001"
    Incoming Subject: "Automated ABC Plant Order: Part Description (02293577)"
    Resulting Subject: "YYYYYY002"
    Incoming Subject: "New 123 B2B Handling Order: Part Description (02267241)"
    Resulting Subject: "YYYYYY003"

    I started out with some code like this:
    Sub EditSubject(Item As Outlook.MailItem) 
        Item.Subject = Replace(Item.Subject, "XXXXXX", "YYYYYY") 
        Item.Save 
    End Sub
    But all that does is change the part number without stripping out all the junk. I need some kind of function that searches the subject for "02246744" or one of the other external part numbers, then replaces the whole subject with the matching internal part number.

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    Try this

    Sub EditSubject(Item As mailItem)
    
        Dim srchString  As String
        Dim origSubj As String
        
        origSubj = Item.subject
        
        srchString = Right(Item.subject, 9)
        srchString = Left(srchString, 8)
        
        Select Case srchString
        
            Case "02246744"
            Item.subject = "YYYYYY001"
                
            Case "02293577"
            Item.subject = "YYYYYY002"
                
            Case "02267241"
            Item.subject = "YYYYYY003"
        
        End Select
        
        If Item.subject <> origSubj Then Item.Save
        
    End Sub
    
    Private Sub EditSubject_test()
        ' open a mailitem first
        EditSubject ActiveInspector.currentItem 
    End Sub
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    Quote Originally Posted by skatonni View Post
    Try this
    Wow, that's amazing! I will try it today!

  4. #4
    Worked perfectly! Solved a huge problem for us.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •