Home > Software design >  How to forward the email that is currently open?
How to forward the email that is currently open?

Time:10-16

I have a macro in Excel to reply to the selected email in Outlook with body, send to and cc in the sheet.

Is it possible to change it to reply the email that is currently opened (not selected)? The best option would be to forward the currently opened email to a new recipient.

Private Sub CommandButton2_Click()
    Dim emailApplication As Object
    Dim emailItem As Object
    
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.ActiveExplorer.Selection.Item(1).ReplyAll
    
    emailItem.cc = Range("R17")
    
    emailItem.Body = Range("B4")
    
    emailItem.Display
    
    Set emailItem = Nothing
    Set emailApplication = Nothing

End Sub

CodePudding user response:

To reference the currently opened email inspector

ActiveInspector

To reference the currently opened email message

ActiveInspector.CurrentItem

You must have an email message currently opened, otherwise ActiveInspector will return an error.

CodePudding user response:

Replace the line

Set emailItem = emailApplication.ActiveExplorer.Selection.Item(1).ReplyAll

with

Set emailItem = emailApplication.ActiveInspector.CurrentItem.ReplyAll

You would need to add a couple of sanity checks (emailApplication.ActiveInspector is not null and emailApplication.ActiveInspector.CurrentItem is MailItem, (check that Class = olMailItem))

  • Related