Outlook VBA code works if run manually, but not complete with hotkey

Alan Chidsey 20 Reputation points
2024-08-14T18:32:35.36+00:00

The below code responds to the sender of the selected email and puts "Trades complete." in the body and displays the sender's original email with the correct formatting, similar as if you click the Reply button.

I have it attached to a hotkey, Ctrl + Shift + H.

Works fine if I go into the Code and click the arrow to run it, and it adds the text "Trades complete." as it should.

However, if I run it by triggering it through the HotKey, ctrl + Shift + R, it generates the reply email but doesn't add the "Trades Complete" text.

Anything I can do to fix this.

Thx.

Sub TradesCompleteEmail()

'Shortcut Key = Shift + Ctrl + R

Dim OutlookApp As Object

Dim OutlookNamespace As Object

Dim CurrentExplorer As Object

Dim SelectedEmail 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)

    senderEmail = SelectedEmail.SenderEmailAddress

    Set CurrentCell = ActiveCell

    

    ' Reply to the selected email

    

    Set ReplyEmail = SelectedEmail.Reply

    

    With ReplyEmail

        .HTMLBody = "Trades Complete." & ReplyEmail.HTMLBody

        .Display

    

    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

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.
4,321 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.