James Oliver
James Oliver

Reputation: 47

Find & Replace text in a email (oft or msg)

Thankyou for stopping by to look at my question.

I’m trying to replace text in an email (oft or msg).

I’m trying to find.

@@TEST1@@ and replace with textbox1.text

@@TEST2@@ and replace with textbox2.text

@@TEST3@@ and replace with textbox3.text

I have found the following to open the email template and this works a treat.

 Dim Outl As Object
    Outl = CreateObject("Outlook.Application")
    If Outl IsNot Nothing Then
        Dim omsg As Object
        omsg = Outl.CreateItemFromTemplate("C:\Testing\EmailSerials.oft")
        omsg.To = "[email protected]"
        omsg.subject = "Hello, Please find your Software"
        omsg.Display(True) 
    End If

i'm new at this so any help or examples would be great.

Manty thanks James

Upvotes: 0

Views: 182

Answers (1)

JayV
JayV

Reputation: 3271

Take a look at [MailItem].Body and String.Replace

I worked on the assumption that the OFT file had content you wish to replace. I also took the opportunity to replace the VB6 code with VB.Net version.

Add a reference to:

Microsoft.Office.Interop.Outlook

And, also add this to the top of the file:

Imports Microsoft.Office.Interop

And then the code to replace the text you are wanting to replace

    Dim Outl As New Outlook.Application

    If Outl IsNot Nothing Then
        Dim omsg As Outlook.MailItem = CType(Outl.CreateItemFromTemplate("C:\Testing\EmailSerials.oft"), Outlook.MailItem)
        omsg.To = "[email protected]"
        omsg.subject = "Hello, Please find your Software"

        omsg.Body = omsg.Body.Replace("@@TEST1@@", textbox1.text)
        omsg.Body = omsg.Body.Replace("@@TEST2@@", textbox2.text)
        omsg.Body = omsg.Body.Replace("@@TEST3@@", textbox3.text)

        omsg.Display(True)
    End If

Upvotes: 1

Related Questions