VBA code to reply to an Outlook email

Alan Chidsey 20 Reputation points
2024-07-30T12:37:01.4633333+00:00

I am trying to create VBA code to reply to an email. I came up with the below code and included the line.

        .Body = "My Text." & vbCrLf & vbCrLf & vbCrLf & SelectedEmail.Body
```which includes the previous messages but the formatting isn't correct.  When I just click the Reply button in outlook, it posts all the previous messages below in the correct formatting.

What modifications do I need to make so the previous messages are included, as they would be if I had clicked the Reply button?

Thx.

>>>>>>


Sub ReplyEmail()

```vba
Dim OutlookApp As Object

Dim OutlookNamespace As Object

Dim CurrentExplorer As Object

Dim SelectedEmail As Object

Dim ForwardedEmail As Object

Dim ReplyEmail As Object

' Create Outlook application object

Set OutlookApp = CreateObject("Outlook.Application")

Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")

Set CurrentExplorer = OutlookApp.ActiveExplorer

' Check if there is an active email selected

If CurrentExplorer.Selection.Count > 0 Then

    Set SelectedEmail = CurrentExplorer.Selection.Item(1)


    

    ' Reply to the selected email

    Set ReplyEmail = SelectedEmail.Reply

    With ReplyEmail

        .Body = "My Text." & vbCrLf & vbCrLf & vbCrLf & SelectedEmail.Body

        .Display ' Use .Send to send the email directly


        

    End With

Else

    MsgBox "No email selected.", vbExclamation

End If

' Clean up

Set ReplyEmail = Nothing

Set SelectedEmail = Nothing

Set CurrentExplorer = Nothing

Set OutlookNamespace = Nothing

Set OutlookApp = Nothing
```End Sub

Outlook
Outlook
A family of Microsoft email and calendar products.
3,444 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,728 questions
0 comments No comments
{count} votes

Accepted answer
  1. ChristyZhang-MSFT 22,416 Reputation points Microsoft Vendor
    2024-07-31T02:07:19.4+00:00

    Hi @Alan Chidsey ,

    Glad to hear this issue has been resolved!

    Thanks for the sharing.


    However, due to a recent update in forum policy, the question author now is not able to accept their own answers.

    So I have written a brief summary of the solution this issue. Please feel free to accept it as the answer, which would benefit others who also has similar issues in forum.

    Issue:

    OP is trying to create VBA code to reply to an email.

    Solution:

    OP Found a solution. changed to :

            ReplyEmail.HTMLBody = ReplyEmail.HTMLBody & SelectedEmail.Reply.HTMLBody
    
            ReplyEmail.Display
    

    And this included the original message in the proper format

    You found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alan Chidsey 20 Reputation points
    2024-07-30T15:47:30.51+00:00

    Found a solution. changed to :

            ReplyEmail.HTMLBody = ReplyEmail.HTMLBody & SelectedEmail.Reply.HTMLBody
    
            ReplyEmail.Display
    

    And this included the original message in the proper format

    0 comments No comments