Consulting

Results 1 to 6 of 6

Thread: Batch Find and Replace Text in Multiple Outlook Emails

  1. #1

    Batch Find and Replace Text in Multiple Outlook Emails

    Hi There Everyone - thanks for this great forum. Alan Chen of DataNumen, suggested I reach out here to find a fix VBA for - "How to Batch Find and Replace Text in Multiple Outlook Emails" - it will not work for me.

    Compile Error - User-defined type not defined. Highlights line item - DIM objMailDocument As Word.Document - cannot get beyond that to see if any other errors - appreciate any help. Thanks again.

    https://www.datanumen.com/blogs/batc...utlook-emails/

    Sub FindReplaceInMultipleEmails()
    Dim strFind, strReplace As String
    Dim objInspectors As Outlook.Inspectors
    Dim objInspector As Outlook.Inspector
    Dim objMail As Outlook.MailItem
    Dim objMailDocument As Word.Document

    'Enter the specific text
    strFind = InputBox("Enter the text for find: (Case Sensitive)")
    strReplace = InputBox("Enter the text for replacement: (Case Sensitive)")

    If Trim(strFind) <> "" Then
    Set objInspectors = Outlook.Application.Inspectors

    For Each objInspector In objInspectors
    If objInspector.CurrentItem.Class = olMail Then
    If objInspector.EditorType = olEditorWord Then
    Set objMail = objInspector.CurrentItem
    Set objMailDocument = objMail.GetInspector.WordEditor

    'Find & replace specific text
    With objMailDocument.Content.Find
    .ClearFormatting
    .Text = strFind
    .Replacement.ClearFormatting
    .Replacement.Text = strReplace
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .Execute Replace:=wdReplaceAll
    End With
    objMail.Save
    End If
    End If
    Next

    MsgBox "Completed!", vbInformation + vbOKOnly
    End If
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,098
    Location
    Have you set a reference to the Word Object Library? See under Tools|References in the VBA Editor.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    You are over complicating this. Select the messages to process and try the following

    Sub FindReplaceInMultipleEmails()
    Dim strFind, strReplace As String
    Dim objMail As Object
    Dim i As Long
    
        'Enter the specific text
        strFind = InputBox("Enter the text for find: (Case Sensitive)")
        strReplace = InputBox("Enter the text for replacement: (Case Sensitive)")
    
        If Trim(strFind) <> "" Then
            For i = 1 To Application.ActiveExplorer.Selection.Count
                Set objMail = Application.ActiveExplorer.Selection.Item(i)
                If TypeName(objMail) = "MailItem" Then
                    objMail.HTMLBody = Replace(objMail.HTMLBody, strFind, strReplace)
                    objMail.Save
                End If
            Next i
        End If
    
        MsgBox "Completed!", vbInformation + vbOKOnly
        Set objMail = Nothing
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Yes, I did, to no avail - and thank your for your kind reply. Hope you and your circle of family and friends are safe and well.

  5. #5
    Thank you for your kind reply. I will try that and report back. Hope this finds you and your circle of family and friends safe and well.

  6. #6
    Oh my gawd . . . . I love you . . . thank you thank you thank you - you have no idea how many hours I spent wrestling with trying to get this to work. I am just a lay-person working toward getting a monthly project as efficient as possible - THANK YOU GRAHAM! Cherie

Posting Permissions

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