PDA

View Full Version : [SOLVED:] Batch Find and Replace Text in Multiple Outlook Emails



madplatter
03-28-2020, 04:48 PM
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/batch-find-replace-text-multiple-outlook-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

macropod
03-28-2020, 06:20 PM
Have you set a reference to the Word Object Library? See under Tools|References in the VBA Editor.

gmayor
03-28-2020, 10:55 PM
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

madplatter
03-29-2020, 03:15 PM
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.

madplatter
03-29-2020, 03:16 PM
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.

madplatter
03-29-2020, 03:28 PM
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