Forward Email with its attachment in Outlook 2010

后端 未结 2 609
时光说笑
时光说笑 2020-12-07 02:26

The below code (I pulled from several sources) now works in that when I receive an email with specific words in the subject line it triggers a script that runs the below.

相关标签:
2条回答
  • 2020-12-07 02:52

    There is an unnecessary condition

    If oExplorer.Selection.item(1).Class = olMail Then
    

    that may cause the forwarding to be bypassed.

    Sub ForwardEmail(item As Outlook.MailItem)
    ' Dim oExplorer As Outlook.Explorer
    Dim oMail As MailItem
    ' Set oExplorer = Application.ActiveExplorer
    
    On Error GoTo Release
    
    ' If oExplorer.Selection.item(1).Class = olMail Then
    
    Set oMail = item.Forward
    oMail.Subject = oMail.Subject
    oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
    oMail.Recipients.Add "email address here"
    
    ' oMail.Save
    oMail.Send
    
    ' End If
    
    Release:
    Set oMail = Nothing
    ' Set oExplorer = Nothing
    End Sub
    
    0 讨论(0)
  • 2020-12-07 02:57

    There is no need to use the Explorer object in the code:

    Sub ForwardEmail(item As Outlook.MailItem)
      Dim oMail As MailItem    
    
      On Error GoTo Release
    
      If item.Class = olMail Then
         Set oMail = item.Forward
         oMail.Subject = oMail.Subject
         oMail.HTMLBody = "Have a nice day." & vbCrLf & oMail.HTMLBody
         oMail.Recipients.Add "email address here"
    
         oMail.Save
         oMail.Send
      End If
     Release:
      Set oMail = Nothing
      Set oExplorer = Nothing
    End Sub
    

    You may find the Getting Started with VBA in Outlook 2010 article helpful.

    0 讨论(0)
提交回复
热议问题