[英]VBA code to send personalized email from excel
I would like to have a code that will send emails using excel table as source. 我想要一个代码,该代码将使用excel表作为源发送电子邮件。 email addresses are in the table, however they are duplicates. 电子邮件地址在表中,但是它们是重复的。
I found a code that generates and email for every row/line, but i need it to generate an email for multiple rows that contain same email address. 我找到了为每行/每一行生成并发送电子邮件的代码,但是我需要它为包含相同电子邮件地址的多行生成一封电子邮件。
PLEASE! 请! Modify the code so that it will generate only 1 email from multiple rows that contain same email address. 修改代码,使其从包含相同电子邮件地址的多行中仅生成1封电子邮件。
*I would do it myself but i do not know how because i do not know how to code in VBA. *我会自己做,但是我不知道怎么做,因为我不知道如何在VBA中编码。
The code i have so far is and it generates email for EVERY line, which is not what i am looking for: 我到目前为止的代码是,它为每一行生成电子邮件,这不是我想要的:
Sub email()
Dim i As Long
Dim OutApp, OutMail As Object
Dim strto, strcc, strbcc, strsub, strbody As String
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
For i = 1 To Range("c65536").End(xlUp).Row
Set OutMail = OutApp.CreateItem(0)
strto = Cells(i, 8)
strsub = "Missing Books"
strbody = "Good day," & vbNewLine & _
"We are missing below books:" & vbNewLine & _
Cells(i, 1) & Chr(32) & Cells(i, 2) & Cells(i, 3) & Cells(i, 5) & Cells(i, 6) & vbNewLine & vbNewLine & _
"brgds," & vbNewLine & _
"Alex"
With OutMail
.To = strto
.Subject = strsub
.Body = strbody
'.Send
.display
End With
On Error Resume Next
Next
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
The final email for email address should look like this: 电子邮件地址的最终电子邮件应如下所示:
to: test@email.com 到:test@email.com
subject: Report! 主题:报告!
Hello! 你好!Here is the overview of the used service: 以下是所用服务的概述:
Period / Number / Company/ Count / Duration * header row 期间/编号/公司/计数/期限* 标题行
201504 / 900654 / ETV / 5 / 89 / * data row, includes email in column H 201504/900654 / ETV / 5/89 / * 数据行,在H列中包含电子邮件
201504 / 900098 / ETV / 2 / 24 / * data row, includes email in column H 201504/900098 / ETV / 2/24 / * 数据行,在H列中包含电子邮件Best wishes, Annu 祝福安努
NOTE: First row is the header row and starting from second row are the data cells. 注意:第一行是标题行,第二行是数据单元格。 They come from a table as does the emails 它们来自表格,电子邮件也来自表格
NOTE: H is the Column for emails - there can be 10 times/lines test@email.com and it needs only 1 email but with the information that comes prior the email cell itself. 注意:H是电子邮件的列-可以有10次/行test@email.com,它只需要1封电子邮件,但其信息位于电子邮件单元格本身之前。
Edit* This is the table 编辑* 这是表格
Please see below to make a string with each row in a column, code will neglect repeating data, then you can use that string in your mailto variable: 请参见下面的内容,使每行中的每一行组成一个字符串,代码将忽略重复的数据,然后可以在mailto变量中使用该字符串:
Sub uniqueSTR()
Dim wb As Workbook
Dim ws As Worksheet
Dim lastRow As Long
Dim mailSTR As String
Set wb = ActiveWorkbook 'Your workbook
Set ws = wb.Sheets("Sheet2") 'Your sheet name
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastRow
If InStr(mailSTR, Range("A" & i).Value) = 0 Then
If mailSTR = "" Then
mailSTR = Range("A" & i).Value
Else
mailSTR = mailSTR & ";" & Range("A" & i).Value '<- Mail seperator is ";"
End If
End If
Next
Range("B1").Value = mailSTR '<- Your result
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.