Search code examples
excelvbaemailoutlookreply

How to Reply to an Email without Overwriting the whole Body Message?


I wrote a program that finds the specific mail in Outlook. Then opens the reply section of the mail and changes the To recipient, CC recipient and adds a body message coming from excel sheet.

When I run the program, it overwrites the whole body message including the previous messages.

How do I add a body message without overwriting the previous messages?

Sub ReplyEmailV1(sMailBody As String, sMailTo As String, sMailCC As String)

    ' initialize variables
    Dim olApp As Outlook.Application
    Dim olNameSpace As namespace
    Dim olFolder As MAPIFolder
    Dim olMail As Outlook.MailItem
    Dim iCountEmail As Integer

    ' set up Outlook Application
    Set olApp = New Outlook.Application
    Set olNameSpace = olApp.GetNamespace("MAPI")
    Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)

    iCountEmail = 1

    '*************************************
    'Loop for each folder items in Outlook
    '*************************************
    For Each olMail In olFolder.Items

        '*************************************
        'Search for the codename in the searchbar of MS Outlook
        '*************************************
        If InStr(olMail.body, "bcqweqw") <> 0 Then

        With olMail.Reply

            '*************************************
            'Get the specified email
            'Open the specified email
            'Open reply
            '*************************************
            .Display

            '*************************************
            'Change To recipient
            '*************************************
            .To = sMailTo

            '*************************************
            'Change CC recipient
            '*************************************
            .CC = sMailCC

            '*************************************
            'Add the body message
            '*************************************
            .body = sMailBody

            '*************************************
            'Send the reply message
            '*************************************
            .Save
            .Send

            '*************************************
            'Close the specified Email
            '*************************************

            '*************************************
            'Increment email count
            '*************************************
            iCountEmail = iCountEmail + 1
        End With

        End If

    '*************************************
    'Loop to the next email found if codename is found
    '*************************************
    Next olMail

    End Sub


    Sub ReplyMultipleEmails()
    'Dim iRow As Integer
    'iRow = 2

    Call ReplyEmailV1(Sheets("Template").Cells(1, 1), Sheets("Data").Cells(2, 7), Sheets("Data").Cells(2, 7))

    'iRow = iRow + 1

End Sub

Solution

  • I tried this and it worked:

     .body = sMailBody & vbLF & .body