-1

I have been tasked to create an automated report system where an report from Google Data Studios are uploaded to specific projects (On a site called Basecamp). The reports always include both a report within the body of the e-mail and an attached PDF file. The are sent to a Gmail account (data studios refuse to schedule towards a non-Google account). The filters within Gmail doesnt really work well with the Basecamp system so I use filters to re-route them towards a Outlook account. There I use rules to send each e-mail towards the correct client within Basecamp.

Here comes the problem, Basecamp shows both the body of the e-mail AND the attached PDF version which makes us show duplicates.

Is there a way to create a macro that first deletes all attachments (or body of an e-mail) and THEN forward the e-mail.

It cant be done manually it have to be a rule that does it automaticaly. Keep in mind that I am not a coder and have never done anything like this so please keep it simple for my dumb brain!

Thank you in advance! Marcus

PS: I found a code that seems to be what I am after.

Public WithEvents ReceivedItems As Outlook.Items
Private Sub Application_Startup()
 Set ReceivedItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub ReceivedItems_ItemAdd(ByVal Item As Object)
 Dim xForwardMail As Outlook.MailItem
 Dim xEmail As MailItem
 On Error Resume Next
 If Item.Class <> olMail Then Exit Sub
 Set xEmail = Item
 If InStrRev(UCase(xEmail.Subject), UCase("kto feature")) = 0 Then Exit Sub 'change subject text to your need
 If xEmail.Attachments.Count = 0 Then Exit Sub
 Set xForwardMail = xEmail.Forward
 With xForwardMail
 .HTMLBody = ""
 With .Recipients
 .Add "[email protected]" 'change address to your own
 .ResolveAll
 End With
 .Send
 End With
End Sub

I am trying to get that code to work, and changes the subject to a specific word and then route it to a final e-mail account that then filters out to correct clients. However the code doesnt seem to work, it DOES forward the e-mail but the attachment is still there. The code was found at https://www.extendoffice.com/documents/outlook/5359-outlook-forward-attachment-only.html#a1

Eugene Astafiev
49.8k3 gold badges28 silver badges50 bronze badges
asked Aug 25, 2021 at 6:53
3
  • 1
    Welcome to SO, please note that SO is not a code writing service. You will need to do your own research, attempt it and post a question with your code attempt when you are stuck at a particular issue. Commented Aug 25, 2021 at 7:38
  • I suppose you can use Application.ItemSend event to detect when an email is sent out and delete the attachments (if any). ItemSend event documentation, MailItem Attachment property documentation. Commented Aug 25, 2021 at 7:40
  • 1
    @RaymondWu There is no need to use the ItemSend event handler. The attachments can be removed before sending forwarded emails. Otherwise, all outgoing emails will be affected and the user will not be able to send out any email with attachments. Commented Aug 25, 2021 at 12:33

1 Answer 1

1

It seems you need to modify the code slightly:

Public WithEvents ReceivedItems As Outlook.Items
Private Sub Application_Startup()
 Set ReceivedItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub ReceivedItems_ItemAdd(ByVal Item As Object)
 Dim xForwardMail As Outlook.MailItem
 Dim xEmail As MailItem
 Dim myattachments as Outlook.Attachments
 On Error Resume Next
 If Item.Class <> olMail Then Exit Sub
 Set xEmail = Item
 If InStrRev(UCase(xEmail.Subject), UCase("kto feature")) = 0 Then Exit Sub 'change subject text to your need
 If xEmail.Attachments.Count = 0 Then Exit Sub
 Set xForwardMail = xEmail.Forward
 
 Set myattachments = xForwardMail.Attachments 
 
 While myattachments.Count > 0 
 myattachments.Remove 1 
 Wend 
 With xForwardMail
 .HTMLBody = ""
 
 With .Recipients
 .Add "[email protected]" 'change address to your own
 .ResolveAll
 End With
 .Send
 End With
End Sub

The Remove method of the Attachments class removes an object from the collection.

answered Aug 25, 2021 at 12:32
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Eugene for the help. It worked, however it seems like it deleted the included report inside the body aswell. I suspect that Google Data studios classifies that report as an attachment aswell. I will continue to tinker but any more help is always much appreciated. It doesnt really matter if the e-mail forwards just the attached PDF or the body of the e-mail (as long as the included report is sent) just not both. Dont really now which is easier to code? Best regards Marcus

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.