Code works when run from code window but not with hotkey.

Alan_CT06

New Member
Joined
Nov 6, 2023
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
The below code copies a few cells from a spreadhsheet and 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 + R.

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
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Ctrl+Shift+R is the shortcut key in outlook for Reply All.
See if it works if you make sure that Excel is the Active Window before hitting the key sequence.
 
Upvote 0
Solution
Alex, Thanks, yes that's exactly what was happening.

When I hit Ctrl + Shift + R I was just firing off the Outlook's Reply All and since my code does the same plus a bit more, I didn't realize it.

Just clicking in Excel, then running gets mine to run, as you indicated.

Thanks, mystery solved.

Alan
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,168
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top