Outlook Email Find and Replace Option

Posted on 2011-04-28
Last Modified: 2012-05-11
I am looking for a way to search through an entire folder of emails and strip out a string of characters inserted from another application. I have found a couple third party options but they do not allow wildcards.

Example of string I'm trying to strip out of 2000+ emails:
(JXTTTTTID: [38521])

The second part starting with the 3 is unique to each email so that's why I need a wildcare like:
Find: (JXTTTTTID: [3*])
Replace With: <blank>

I've had no luck. Maybe this needs to be scripted somehow.

Thanks in advance,
Question by:amait
    LVL 76

    Accepted Solution

    Hi, amait.

    This should do it.  Follow these instructions to use it.

    1.  Start Outlook
    2.  Click Tools > Macro > Visual Basic Editor
    3.  If not already expanded, expand Microsoft Office Outlook Objects
    4.  If not already expanded, expand Modules
    5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
    6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
    7.  Edit the code as needed.  I included comments wherever something needs to or can change
    8.  Click the diskette icon on the toolbar to save the changes
    9.  Close the VB Editor
    10. Open a folder and select one or more messages to process
    11. Run the macro to process the selected messages.

    Please be sure to try this against a few test items before you put it into production.
    Sub amait()
        Dim strResult As String
        For Each olkItm In Application.ActiveExplorer.CurrentFolder.Items
            strResult = SearchAndReplace(olkItm.HTMLBody, "\(JXTTTTTID: \[3\d{1,4}\]\)", "")
            If strResult <> "" Then
                olkItm.HTMLBody = strResult
            End If
    End Sub
    Function SearchAndReplace(strItem As String, strSearchText As String, strReplaceText As String) As String
        Dim objRegEx As Object
        Set objRegEx = CreateObject("VBscript.RegExp")
        With objRegEx
            .IgnoreCase = False
            .Pattern = strSearchText
            .Global = True
        End With
        If objRegEx.Test(strItem) Then
            SearchAndReplace = objRegEx.Replace(strItem, strReplaceText)
            SearchAndReplace = ""
        End If
        Set objRegEx = Nothing
    End Function

    Open in new window


    Author Comment

    Thank you so much for your time BlueDevilFan. We began writing a script but I will try what you have here and get back to you.

    Thanks again for everything. I will award points shortly.
    LVL 76

    Expert Comment

    by:David Lee
    Hi, Dave.

    I'm a little confused.  If the solution worked, then why are you closing the question by accepting your comment and not giving any points?

    Author Comment

    I intended to award all points to BlueDevilFan but made a mistake.
    LVL 76

    Expert Comment

    by:David Lee
    No problem.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Learn more about the importance of email disclaimers with our top 10 email disclaimer DOs and DON’Ts.
    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now