Trying to create personalized emails with multiple attachments - excel

Updated per suggestions from Jeeped:
I am looking for a method of creating a set of emails fitting the following parameters:
each email will be personalized to the recipient and based off a template letter set by my supervisor.
There will be a greeting line with their name and title, along with the names of the departments they oversee.
each email will have a set of documents specific to that recipient.
they should be saved to file for final inspection before they are sent.
column 5 that is not referenced in the code below is the column containing the department name.
The closest I have come is the following code:
Sub send_template_w/attachments()
On Error Resume Next
Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.Mailitem
Set omail =.Createitem(olMailitem)
Dim I As Long
For i=2 To Range(“a100”).End(xlUp).Row
With omail
.Body = “Dear “ & Cells(i,1).Value
.To = Cells(i,2).Value
.CC = Cells(i,3).Value
.Subject = Cells(i,4).Values
.Attachments.Add Cells(i,6)
.Attachments.Add Cells(i,7)
.SaveAs Environ("HOMEPATH") &; "\My Documents\" & Cells(i,2).Value
End With
Next
End Sub
So far, this code will generate and save an email but what I want to do is use a present email template for these emails--either by adding the greeting at the beginning and department name into the body of the the email to be sent out. Can this be done through a word or Outlook document and if so, how?

Create a model of the mail. "Save As" to an .oft file. For example MyTemplate.oft
Instead of
Set omail =.Createitem(olMailitem)
there is
Set omail = o.CreateItemFromTemplate("C:\MyTemplate.oft").
To add the entries from the Excel sheet you could include unique placeholders in the body of the template then Replace with Excel values.

Related

Use VBA to send mass email

I'm taking over a workbook created from the last employee. There's a specific coding that I cannot figure out where it states to grab the email list from to plug into the BCC line.
I'm not finding anywhere in the code that states that but the macro somehow works. It is able to extract the email addresses and plug them into the bcc line on the email template. I see that they specified bc_r but I don't see any worksheets name RecipientEmails. The only worksheet with a list of email addresses in this workbook that I see is in the same tab as the command button to pull this email template. The email address is listed on this sheet under column T. So, I'm unsure why the code says c1. Please review the codes below and if you can guide me, I'd appreciate it.
Sub esendemail()
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
bc_r = ""
For Each c1 In Range("RecipientEmails")
bc_r = bc_r & ";" & c1.Value
Next c1
With newEmail
.To = ""
.BCC = bc_r
.Subject = "Welcome to the team!"
.Body = "[Greetings]" & vbCrLf & " " & vbCrLf & "Practice Name:"
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Set pageEditor = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
End Sub
The email addresses are retrieved in the named range:
bc_r = ""
For Each c1 In Range("RecipientEmails")
bc_r = bc_r & ";" & c1.Value
Next c1
Check out the RecipientEmails named range in your worksheet. See Create a named range from selected cells in a worksheet for more information about named ranges.
Also I'd recommend using the Recipients property for setting up recipients on the Outlook item. You can read more about that approach in the article I wrote for the technical blog, see How To: Fill TO,CC and BCC fields in Outlook programmatically for more information.

Need to send separate emails to multiple recipients from excel list

I have an excel list of people. I want to send emails to all as separate mails. Need to dynamically change the subject,body and recipients.
I have tried using vba and doing it. But i do not know how to dynamically change the subject and body. Also how can i enter multiple lines in the body?
i do not want all recipients to be sent the same mail.Image shows the field names in excel
I need to have first name and first letter of last name in subject. And in body first line includes Hi firstname..And personal mail goes in the "to" field and professional as "cc"
Try it this way:
'Initialize objects
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
.To = ws.Range("A1") 'Assuming TO mail addresses are located here and separated with ";"
.CC = ws.Range("B1") 'Assuming TO mail addresses are located here and separated with ";"
.Subject = ws.Range("C1") 'Assuming subject is declared here
.HTMLBody = ws.Range("D1") 'Assuming body is declared here
If address_attachment_line.Value <> "" Then
.Attachments.Add FilePath & FileName
End If
.Display
End With
Through storing the dynamic information in the ranges referenced by the code, you can control the single mails.
Furhermore to make linebreaks in the body text, just use the tag <br> as it is interpreted as HTML content.
Hope this helps you!

Macros to Send attach different files to multiple individual emails, vba

I am a computer technician, not a programmer, but in my new job I have been asked to finish a macros in excel (vba).
It consists of a list of id card numbers in one of the columns (the number of cells is variable each time it is used, one day you can put 20 people and another 12 for example), and emails in another column.
In a folder there are some pdf documents whose name is the id card of the person that appears in the excel.
What they ask me is that, being ordered the id card in alphabetical order, take the id card and email. The id card will serve to find your corresponding pdf and add it as an attachment with the idea of sending it by email, to whom? there the cell is used with the email data. This has to be done with each of the existing rows, take pdf file to attach it and send email to the address of that same row until there are no more rows on the sheet.
Can someone tell me how to do that or tell me the functions I need?
Thank you.
Graphical idea:
The macro is currently set to .Display the email and not send. After you have finished running tests you will want to change this to .Send to actually send the email.
You will also need to update the value of strLocation. Inside the quotes is where you will need to put the location of the folder that houses all of your target PDFs.
The order of your cells doesn't matter here as long as each row is associated to one individual.
Hopefully these emails are internal - you should not use this for external mailing lists as you cannot offer the option to unsubscribe. Outlook may flag/ban your account if you are suspected of spam.
This assumes the values in Column C are actual email addresses that will be recognized as is by Outlook. (urdearboy#email.com)
Sub CorpCard()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("C").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*#?*.?*" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.SentOnBehalfOfName = "[your associated Outlook email here]"
.to = cell.Value
.Subject = "Subject goes here"
.Body = "Hi " & Range("B" & cell.Row).Value & "," _
'Body to be patsed here
strLocation = "C:\Users\urdearboy\Desktop\File Name\" & Cells(cell.Row, "D").Value & ".pdf"
.Attachments.Add (strLocation)
.Display
'.Send
End With
On Error GoTo 0
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub

Open an Excel attachment in Outlook 2010 using VBA

I would like to know how to Open an Excel attachment in my inbox Outlook 2010 with VBA code.
I would like the code to:
Check for a specific subject that does not change "Test"
Check if that email is read or unread and if it is unread to use that one
I have a rule in place where the emails are stored in a subfolder based on the subject but I can change it to go back to the main inbox
I would really appreciate if you can explain what the code is doing as I am not familiar with the Outlook connection bit.
This is what I have pulled together from various sites including stackoverflow it does the job.
It seems to run for emails that have RE: in the subject for the 10PM and 5PM emails. I explicitly named subject of the two emails. Can anyone help me?
I am not familiar with declaring variables for Outlook objects. Have I declared the variables correctly?
I would also like to copy the data within each file and paste it into another workbook. I would like the data to be pasted underneath the first data from each work book so it will need to find the last row and paste it 1 row below the last row for each data on each workbook onto the open workbook which is stored in another path.
.
Sub DownloadAttachmentFirstUnreadEmail()
Const olFolderInbox = 6
Const AttachmentPath As String = "C:\My Documents\Outlook Test\"
Dim oOlAtch As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set objFolder = objFolder.Folders("**CLIENT ISSUES**").Folders("*Daily Reports").Folders("1. Open Trade Report")
Set colItems = objFolder.Items
Set colFilteredItems1 = colItems.Restrict("[Unread] = True AND [Subject] = '10PM FXC Email notification for Martin Currie'")
Set colFilteredItems2 = colItems.Restrict("[Unread] = True AND [Subject] = 'FXC Email notification for Martin Currie Funds'")
'~~> Check if there are any actual unread 10PM FXC emails
If colFilteredItems1.Count = 0 Then
MsgBox "NO Unread 10PM Email In Inbox"
Else
'~~> Extract the attachment from the 1st unread email
For Each colItems In colFilteredItems1
'~~> Check if the email actually has an attachment
If colItems.Attachments.Count <> 0 Then
For Each oOlAtch In colItems.Attachments
'~~> save the attachment and open them
oOlAtch.SaveAsFile AttachmentPath & oOlAtch.Filename
Set wb = Workbooks.Open(Filename:=AttachmentPath & oOlAtch.Filename)
Next oOlAtch
Else
MsgBox "10PM email doesn't have an attachment"
End If
Next colItems
End If
'~~> Check if there are any actual unread FXC Email emails
If colFilteredItems2.Count = 0 Then
MsgBox "NO Unread 5PM Email In Inbox"
Else
'~~> Extract the attachment from the 1st unread email
For Each colItems In colFilteredItems2
'~~> Check if the email actually has an attachment
If colItems.Attachments.Count <> 0 Then
For Each oOlAtch In colItems.Attachments
'~~> save the attachment and open them
oOlAtch.SaveAsFile AttachmentPath & oOlAtch.Filename
Set wb = Workbooks.Open(Filename:=AttachmentPath & oOlAtch.Filename)
Next oOlAtch
Else
MsgBox "5PM email doesn't have an attachment"
End If
Next colItems
End If
End Sub
First of all, I'd suggest starting from the Getting Started with VBA in Outlook 2010 article in MSDN.
You can assign a VBA macro to the Outlook rule, it should look like the following one:
public sub test(mail as MailItem)
'
end sub
where you can check out the mail object. It looks like you need to check out the Subject, UnRead and Attachments properties of the MailItem class.

Send an email from Excel 2007 VBA using an Outlook Template & Set Variables

I have a list of data, let's say client information (Name, Email, Amount Owing etc.), stored in an Excel worksheet. My aim is to click a button in Excel and send each client their information in an Outlook Template.
create a mail object
set the mail object to the template file
setting and then filling in the template with data about the current client - mostly stuck here, not sure how to specify variables in a template and then relate to them in VBA
save to drafts for later review/send
e.g. Dear << clientname >> = Dear John Smith
My code thus far:
Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Set myOlApp = CreateObject("Outlook.Application")
Set MyItem = myOlApp.CreateItemFromTemplate("C:\egTemplate.oft")
With MyItem
.To = Worksheets("Clients").Range(1, 2)
.Subject = "Monthly bill"
'Refer to and fill in variable items in template
.Save
End With
Set MyItem = Nothing
Set MyOlApp = Nothing
Here is what you can do :
With MyItem
'Refer to and fill in variable items in template
.Body = Replace(.Body, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
or, if your mail is in HTML:
With MyItem
'Refer to and fill in variable items in template
.HTMLBody = Replace(.HTMLBody, "<< clientname >>", Worksheets("Clients").Range(1, 2))
End With
Tested successfully on Excel / Outlook 2007
This is a perfect job for mail merge. If you want to do it programmatically, see
Mail Merge in Word+Excel using VBA
Or you could simply do it manually (from Word), inserting merge fields and then selecting your workbook as the data source. You can merge to email and Outlook will send out personalized emails to each recipient's email using the information from each row/record.

Resources