VBA to forwarding emails in Outlook

0

I'm trying to create a vba that can forward emails I received. The only problem is that the emails I'd like to forward have different subjects. Only the beginning is the same. This is how far I've gotten with it(this should be inserted to ThisOutlookSession). Someone could help me please?

Public WithEvents objInbox As Outlook.Folder
Public WithEvents objInboxItems As Outlook.Items
Private Sub Application_Startup()
    Set objInbox = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
    Set objInboxItems = objInbox.Items
End Sub

Private Sub objInboxItems_ItemAdd(ByVal item As Object)
    Dim objMail As Outlook.MailItem
    Dim objForward As Outlook.MailItem

    If TypeOf item Is MailItem Then
       Set objMail = item

       'If it is a specific new email'
       If (objMail.Subject = "Offer Response Received") Then



           Set objForward = objMail.Forward
           'Customize the forward subject, body and recipients'
           With objForward
                .Subject = "Offer accepted"
                .HTMLBody = "<HTML><BODY>Please proceed. </BODY></HTML>" & objForward.HTMLBody
                .Recipients.Add ("")
                .Recipients.Add ("")
                .Recipients.ResolveAll
                .Importance = olImportanceHigh
                .Send
           End With
       End If
    End If
End Sub

MarcellNoel26

Posted 2019-12-10T10:28:01.907

Reputation: 1

Is there a reason you are not using Outlook clientside rules? – LPChip – 2019-12-10T10:56:34.827

Yes, we have these kind of emails from 2016 - 2019 and we don't want them to be sent again. – MarcellNoel26 – 2019-12-10T14:13:04.220

Answers

0

You can check the subject for the text common the applicable mail.

Private Sub objInboxItems_ItemAdd(ByVal item As Object)

    Dim objMail As MailItem
    Dim objForward As MailItem

    Dim beginStr As String
    Dim lenBegin As Long

    beginStr = "the common text at beginning of applicable mail"
    lenBegin = Len(beginStr)

    If TypeOf item Is MailItem Then

        Set objMail = item

        'New email where the "beginning is the same"
        If Left(objMail.Subject, lenBegin) = beginStr Then

            Set objForward = objMail.Forward

            'Customize the forward subject, body and recipients'
            With objForward
                .Subject = "Offer accepted"
                .HTMLBody = "<HTML><BODY>Please proceed. </BODY></HTML>" & objForward.HTMLBody
                '.recipients.Add ("")
                '.recipients.Add ("")
                .recipients.ResolveAll
                .Importance = olImportanceHigh
                .Display    '.Send
            End With

        End If
    End If

End Sub

niton

Posted 2019-12-10T10:28:01.907

Reputation: 1 724