Home > Software design >  Need to change vba to forward the email that is currently opened
Need to change vba to forward the email that is currently opened

Time:10-15

I have a macro in excel to reply to the selected email in outlook with body, send to and cc in the sheet. Is this 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

Thank you,

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