0

I created a function that replaces a placeholder in MailItem body (oft). It is working in my machine but not the my client machine. Where both using MS Office 365. Anyone resolved this issue?

Sub ReplaceInEmailBody(MyItem As Object, stringToReplace As String, str As String)
 Dim oDoc As Word.Document
 Dim myInspector As Outlook.Inspector
 Set myInspector = MyItem.GetInspector
 
 Set oDoc = myInspector.WordEditor ----> generates Application-defined or object defined error

    With oDoc.Content.Find
        With .Replacement
        .ClearFormatting
        .Font.Bold = True
        End With
        .Execute FindText:=stringToReplace, ReplaceWith:=str, Format:=True, _
        Replace:=wdReplaceAll
    End With

End Sub
  • Does this answer your question? [MailItem.GetInspector.WordEditor in Office 2016 generates Application-defined or object defined error](https://stackoverflow.com/questions/45376329/mailitem-getinspector-wordeditor-in-office-2016-generates-application-defined-or) – niton Jul 29 '22 at 16:21

1 Answers1

0

If the message is not currently being displayed, you can run into troubles with the Word editor, especially if you are processing a large number of messages.

Try to replace text in the MailItem.HTMLBody property instead:

MyItem.HTMLBody = Replace(MyItem.HTMLBody, stringToReplace, str)
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • The message is displayed before call that function. I use MailItem.Display. MailItem.HTMLBody is not applicable in my use case because I'm modifying rich text type of email. – supermar May 27 '22 at 18:32