wittman
wittman

Reputation: 305

Modify the current email body outlook

I want to replace in the active email the "text1" with "text2" . Here is my code:

Sub Custmod()
Dim olItem As Outlook.MailItem
Dim objOL As Outlook.Application
Dim olOutMail As Outlook.MailItem
Dim sText As String
Dim vText As String
Dim strBody As String
Set objOL = Application
Set objItem = objOL.ActiveInspector.CurrentItem
For Each olItem In Application.ActiveExplorer.Selection
    sText = olItem.body
    vText = Split(sText, Chr(13))
    For i = 1 To UBound(vText)
        If InStr(1, vText(i), "TEXT1") Then
            olItem.body = Replace(vText(i), "TEXT2", "")
    Next i
End Sub

Any help is welcomed. Thank you.

Upvotes: 0

Views: 3538

Answers (1)

niton
niton

Reputation: 9199

It looks like you are trying to use an array when not needed.

Replace works on multiple instances of the same word.

Option Explicit

Sub Custmod()

Dim olItem As mailItem

Set olItem = CreateItem(olMailItem)
olItem.body = "TEXT1" & Chr(13) & "Here is some stuff." & Chr(13) & "TEXT1 again."
olItem.Display
MsgBox olItem.body

olItem.body = Replace(olItem.body, "TEXT1", "TEXT2")
MsgBox olItem.body

Set olItem = Nothing

End Sub

Upvotes: 2

Related Questions