Macro/VBA to Reply All, with the original attachments

Status
Not open for further replies.

lsellin1

New Member
Outlook version
Outlook 2019 64-bit
Email Account
Office 365 Exchange
My company uses an ERP system that emails our customers their invoices as attachments. For whatever reason, these emails from the ERP system are not delivered to Gmail or Yahoo email accounts, but we have included ourselves as recipients and get it without issue. From there we can Reply All, include the Attachment, and our customers receive their invoices. This is an entirely manual process right now. To get around this, I am trying to create a rule that when we receive the emails, the ReplyAll with Attachments occurs automatically.

I thought I had it with the VBA code below, but it when it is triggered by the rule, it Replies All with Attachment based on the email I have selected in Outlook and the time the rule is triggered, and not based on the email that triggers the rule. Please help me out. I am an accountant not a programmer.

Code:
Sub ReplyAllWithAttachments(Item As Outlook.MailItem)
'ReplyAllWithAttachments

Dim oReply As Outlook.MailItem
Dim oItem As Object
Set oItem = GetCurrentItem()
If Not oItem Is Nothing Then
Set oReply = oItem.ReplyAll
CopyAttachments oItem, oReply
oReply.Display
oReply.Send
    
End If

Set oReply = Nothing
Set oItem = Nothing
End Sub
Function GetCurrentItem() As Object
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = Application.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = Application.ActiveInspector.CurrentItem
End Select
End Function
Sub CopyAttachments(oSourceItem, oTargetItem)
Set oFso = CreateObject("Scripting.FileSystemObject")
Set fldTemp = oFso.GetSpecialFolder(2) 'Temporary Folder
sPath = fldTemp.Path & "\"
For Each oAtt In oSourceItem.Attachments
sFile = sPath & oAtt.FileName
oAtt.SaveAsFile sFile
oTargetItem.Attachments.Add sFile, , , oAtt.DisplayName
oFso.DeleteFile sFile
Next
Set fldTemp = Nothing
Set oFso = Nothing
End Sub
 
Replies All with Attachment based on the email I have selected in Outlook and the time the rule is triggered, and not based on the email that triggers the rule.
You mean the reply is using the wrong From address?

You need to use
Set oReply = oItem.ReplyAll
oReply.SentOnBehalfOfName = "alias@domain.com"


 
Diane-
First off, thanks for your reply. I was really hoping to get a response from you, since I have read countless forum posts and your replies, and you are obviously an expert at this.

That being said, no, the reply using the wrong From address is not what I mean. The wrong email is being selected, and replied all to. I think the issue is in the portion of code, " Set oItem = GetCurrentItem() ".

From my testing, what I can tell is that this sets the currently selected email as the item to use when replying all.

For example:

When I first open Outlook, if I don't click on an email, no email is selected. If an email is received that triggers the rule, oItem is set to nothing and the script ends without replying to any email.
1600436547236.png


If I have clicked on an email, it is selected. If an email is received that triggers the rule, oItem is set to the previously selected email and the reply all is based off of that email and not the new email that triggered the rule. Example being the screenshot below. I have the "Welcome to SAP Analytics Cloud" email selected. If the "9:00 AM Distribution" email is received and triggers the rule, the "Welcome to SAP Analytics Cloud" email is set to the oItem and the reply all of that email is sent to noreply-sac+notification@sap.com. What I want to do is to get the email that triggers the rule to be selected and set as the oItem.
1600437447118.png


The code works perfectly if I run it as a Macro instead of a script. But this requires selecting each email individually, running the Macro, and then going to the next email. For this to be a viable option, I would need to adjust the code so that I can select multiple emails and have it loop through the code for each email in the selection. (Which I don't know how to do)

Thanks again.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
G VBA Macro Using Outlook 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
R VBA macro - new message Outlook VBA and Custom Forms 3
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
S VBA Macro - Run-time error '424': object required - Help Please Outlook VBA and Custom Forms 3
B VBA Macro for assigning multiple Categories to an email in my Inbox Outlook VBA and Custom Forms 1
M Slow VBA macro in Outlook Outlook VBA and Custom Forms 5
M VBA macro for Inbox's attachments search Outlook VBA and Custom Forms 0
Y VBA Macro to highlight some Keyword in mail body? Outlook VBA and Custom Forms 3
J Macro only runs within VBA Editor Outlook VBA and Custom Forms 2
D VBA macro printing attachments in shared mailbox Outlook VBA and Custom Forms 1
G VBA/Macro to remove page colour when replying or forwarding email Outlook VBA and Custom Forms 2
S Outlook 7 VBA macro for multiple filing Outlook VBA and Custom Forms 1
M Outlook VBA Macro that could retrieve/display the (From, Subject, Date Received) Outlook VBA and Custom Forms 2
Philip Rose Recreating a WORD Editing Macro to use in Outlook VBA Outlook VBA and Custom Forms 4
T Random problem in VBA macro Outlook VBA and Custom Forms 6
M recover deleted items vba macro Outlook VBA and Custom Forms 2
P Vba script including macro appears in rules but wont run Outlook VBA and Custom Forms 6
S VBA Macro to VBScript Outlook VBA and Custom Forms 1
L VBA Macro to flag sent emails Using Outlook 1
B Outlook vba macro Using Outlook 1
B Auto BCC VBA macro: how to add exceptions? Using Outlook 28
R VBA Macro to VBScript in a form- Help Please! Using Outlook 10
D VBA Macro Works in 2007 but errors in 2010 Using Outlook 2
E Word macro running from OL VBA throwing error on Word SaveAs?? Outlook VBA and Custom Forms 8
J Outlook custom form - VBS call VBA macro Outlook VBA and Custom Forms 3
N VBA Procedure (or Macro) to Stop Outlook from Closing? Outlook VBA and Custom Forms 2
M Run external program from Outlook VBA macro Outlook VBA and Custom Forms 5
D New Macro / VBA Outlook VBA and Custom Forms 7
J Specify Font Name or Face using VBA Macro in Word Outlook VBA and Custom Forms 2
S Custom VBA forms in Outlook--how to call w/macro? Outlook VBA and Custom Forms 1
S macro ist just worling wenn vba editor is open! Outlook VBA and Custom Forms 1
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 2
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 1
S macro error 4605 Outlook VBA and Custom Forms 0

Similar threads

Back
Top