Forward emails with attachments to all recipients - vba

I receive emails containing Excel reports as an attachment. They are all from the same sender but have different recipients on each email.
An example:
Email #1
From: John#gmail.com To: me#gmail.com;mike#gmail.com
+1 attachment Excel file
Email #2
From: John#gmail.com To: me#gmail.com;jessica#gmail.com
+1 attachment Excel file
I need to forward those emails to the recipients again WITH the attachment file.

You could create a macro rule, when you receive an email from specific sender run a script.
About automatically save attachment, please refer to this code:
Sub Save_Attachment(olItem As Outlook.MailItem)
Dim olAttch As Outlook.attachment
Dim sPath As String
Dim acount
Dim objMsg As MailItem
Dim recips As Outlook.Recipients
Dim recip As Outlook.Recipient
For Each olAttch In olItem.Attachments
If olAttch.UnRead = True Then
If olAttch.SenderEmailAddress = "v-shuail#microsoft.com" Then
Set acount = olAttch.Attachments.Count
If acount > 0 Then
Set objMsg = Application.CreateItem(olMailItem)
Set recips = olAttch.Recipients
With objMsg
.Subject = "This is the subject"
.Attachments.Add ("path-to-file.docx")
For Each recip In recips
.Recipients.Add (recip)
Next
.Send
End With
Set objMsg = Nothing
End If
End If
End If
Next
Set olAttch = Nothing
End Sub

Related

Redirect selected emails and make reply-to address same as original sender

I receive support requests in a shared exchange mailbox in Outlook 2013.
I forward the mails, which I am supposed to treat, to an external ticketing system. Before forwarding, I copy the original sender's address into the reply-to address.
The following macro would be perfect if:
the original sender's address would be copied into the reply-to address, and
if it could set the category of the original mail to "myname" and mark it as done.
Sub BatchRedirectEmails()
Dim objSelection As Outlook.Selection
Dim i As Long
Dim objMail As Outlook.MailItem
Dim objRedirectMail As Outlook.MailItem
'Get all selected emails
Set objSelection = Application.ActiveExplorer.Selection
If Not (objSelection Is Nothing) Then
For i = objSelection.Count To 1 Step -1
If TypeOf objSelection(i) Is MailItem Then
Set objMail = objSelection(i)
'Redirect each email
Set objRedirectMail = objMail.Forward
With objRedirectMail
'Add more recipients as per your needs
.Recipients.Add ("john#datanumen.com")
.Recipients.Add ("abby#datanumen.com")
.Recipients.Add ("coral#datanumen.com")
.Recipients.Add ("david#datanumen.com")
.Recipients.ResolveAll
.Subject = objMail.Subject
.HTMLBody = objMail.HTMLBody
.Send
End With
End If
Next
End If
End Sub

Reply all with attachment

I have this code to a vba outlook macro to reply all.
Sub my_test()
Dim objItem As Object
Dim mail As MailItem
Dim replyall As MailItem
Dim templateItem As MailItem
For Each objItem In ActiveExplorer.Selection
If objItem.Class = olMail Then
Set mail = objItem
Set replyall = mail.replyall
Set templateItem = CreateItemFromTemplate("C:\template.oft")
With replyall
.HTMLBody = templateItem.HTMLBody & .HTMLBody
.Display
End With
End If
Next
End Sub
I am trying to add a functionality so that when the original email brings an attachment (docx, pdf), when I reply all using this macro it will also use the original attachment and place it as an attachment in the reply all email.
How can I achieve this?
Forward then populate the .To with what would appear in a ReplyAll.
Option Explicit
Sub my_test()
Dim objItem As Object
Dim mail As MailItem
Dim forwardMail As MailItem
Dim templateItem As MailItem
For Each objItem In ActiveExplorer.Selection
If objItem.Class = olMail Then
Set mail = objItem
Set forwardMail = mail.Forward
Set templateItem = CreateItemFromTemplate("C:\template.oft")
With forwardMail
.HTMLBody = templateItem.HTMLBody & .HTMLBody
.To = mail.replyall.To
.Display
End With
End If
Next
End Sub

Set an email has replied - vba

I have macro to forward an email with the original attachment to everyone which is involved in the original email chain.
Sub my_test()
Dim objItem As Object
Dim mail As MailItem
Dim forwardMail As MailItem
Dim templateItem As MailItem
For Each objItem In ActiveExplorer.Selection
If objItem.Class = olMail Then
Set mail = objItem
Set forwardMail = mail.Forward
Set templateItem = CreateItemFromTemplate("C:\template.oft")
With forwardMail
.HTMLBody = templateItem.HTMLBody & .HTMLBody
.To = mail.replyall.To & mail.replyall.CC
.Display
End With
End If
Next
End Sub
Is it possible to mark this email has "replied" instead of "forwarded" email?
yes you need only to change Set forwardMail = mail.Forward to Set forwardMail = mail.Reply
You should also change name of variable forwardMail to replyMail and change all variables in code. full code below.
Sub my_test()
Dim objItem As Object
Dim mail As MailItem
Dim replyMail As MailItem
Dim templateItem As MailItem
For Each objItem In ActiveExplorer.Selection
If objItem.Class = olMail Then
Set mail = objItem
Set replyMail = mail.Reply
Set templateItem = CreateItemFromTemplate("C:\template.oft")
With replyMail
.HTMLBody = templateItem.HTMLBody & .HTMLBody
.To = mail.replyall.To & mail.replyall.CC
.Display
End With
End If
Next
End Sub
If you mean you want to change the icon to the one that represents "replied", you can change it in the following way...
' Set property PR_ICON_INDEX to 261
objItem.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x10800003", 261
objItem.Save

Macro doesn't show open mail

Below is my macro to send an email but it does not open a new email .
This macro is in the outlook rules.
Have you any ideas?
Sub sendemail()
Dim ns As NameSpace
'Dim newMail As Outlook.MailItem
Set ns = GetNamespace("MAPI")
Dim newMail As MailItem
Set newMail = Application.CreateItem()
With newMail
.To = "aaa#bbb" <--adress to whitch I want to send an email
.Subject = "test"
.Display
End With
Set newMail = Nothing
End Sub
first you need to add up the outlook reference:
There you go:
then:
Sub sendemail()
Dim outlook As New outlook.Application
Dim newMail As outlook.MailItem
Set newMail = outlook.CreateItem(olMailItem)
With newMail
.To = "email#address.com" ' <--adress to whitch I want to send an email
.Subject = "test from Steph"
.Display
.Send
End With
Set newMail = Nothing
outlook.Quit
Set outlook = Nothing
End Sub
Argument must be type MailItem or MeetingItem for the subroutine to be available in Outlook Rules Wizard.
Try this.
Option Explicit
Sub SendEmail(NewMail As Outlook.MailItem)
Set NewMail = Application.CreateItem(olMailItem)
With NewMail
.To = "aaa#bbb" '<--adress to whitch I want to send an email
.Subject = "test"
.Display
End With
Set NewMail = Nothing
End Sub
Youre not specifying the item to create. See here:
Private Sub SendEmail(ByVal workcenter As Integer, ByVal time As Date)
Dim objApp As Object
Dim objEmail As Object
Set objApp = CreateObject("Outlook.Application")
Set objEmail = CreateObject("Outlook.MailItem")
With objEmail
.To = "emailexampe#website.com"
.Subject = "Multiple Shop Orders run for line " & workcenter & " at " & time
.body = "TEST"
.display
End With
Set objEmail = Nothing
Set objApp = Nothing
End Sub
Source: http://www.vbforums.com/showthread.php?536558-RESOLVED-Outlook-late-binding
this version also uses late binding, so you dont need any additional references.

Macro to delete an email

I have created a macro that forwards an email on to a recipient once a button is clicked. However, I want the macro to also delete the email (sending it to the recycle bin).
Here is the current code. This currently works and forwards the email.
Sub forwardEmail()
Dim oExplorer As Outlook.Explorer
Dim oMail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set oMail = oOldMail.forward
oMail.Recipients.Add "Recipients email goes here"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
oMail.Send
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
End If
Else
MsgBox "Not a mail item"
End If
End Sub
I thought by adding oMailItem.Delete to the code would work but it does not.
It wasn't clear to me which email you wanted deleted, the original email or the forwarded email from Sent items - so these mods provide both options.
Sub forwardEmail()
Dim oExplorer As Outlook.Explorer
Dim oMail As Outlook.MailItem
Dim oOldMail As Outlook.MailItem
Set oExplorer = Application.ActiveExplorer
If oExplorer.Selection.Item(1).Class = olMail Then
Set oOldMail = oExplorer.Selection.Item(1)
Set oMail = oOldMail.Forward
oMail.Recipients.Add "spam_me"
oMail.Recipients.Item(1).Resolve
If oMail.Recipients.Item(1).Resolved Then
'delete forwarded email from sent items
oMail.DeleteAfterSubmit = True
oMail.Send
'delete original email from inbox
oOldMail.Delete
Else
MsgBox "Could not resolve " & oMail.Recipients.Item(1).Name
End If
Else
MsgBox "Not a mail item"
End If
End Sub