Excel宏在多个单独的电子邮件中发送特定信息

时间:2016-05-31 19:38:10

标签: excel vba email outlook

所以我需要一些帮助。我正在尝试更新我们公司用于管理客户的Excel工具。我目前需要帮助更新该工具的发送电子邮件功能。因此,员工将拥有他们发送电子邮件所需的客户列表,每个客户都将拥有特定信息,如姓名,客户编号等,员工应该能够点击内置的“发送电子邮件”宏按钮,Outlook中将填充不同的电子邮件,其中包含电子邮件收件人,标题和正文中内置的每个客户的特定信息。

我目前被困住了。例如,我将选择3个客户端发送电子邮件,单击“发送电子邮件”按钮,我将收到3封电子邮件(我应该)。第一封电子邮件将在客户#1的正确位置提供所有正确的信息。但是,电子邮件#2将拥有正确的电子邮件收件人&电子邮件标题,但电子邮件正文将具有客户端#2的正确信息,并且在电子邮件正文中也将是客户端#1的所有电子邮件正文信息。对于电子邮件#3,它将具有正确的收件人和标题,但是正文将包含客户端#3,然后是客户端#2,然后是客户端#1的正文信息。

所以我知道我需要通过电子邮件正文的某种循环来修复它,但我已经在这方面工作了很长时间,我再也看不到它了。我已删除任何敏感信息并放入占位符,但我认为您应该明白这一点。

如果您有任何疑问,请与我们联系。

Sub SendEMail()
Dim Email As String
Dim Subj As String
Dim Msg As String
Dim URL As String
Dim r As Integer
Dim x As Double
Dim OApp As Object
Dim OMail As Variant
Dim Signature As String
Dim strbody As String



strbody = "<html><body>"

With Sheets("Email").Select
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
End With

For r = 2 To lastrow


Set OApp = CreateObject("Outlook.Application")
Set OMail = OApp.CreateItem(0)

'       Get the email address
Sheets("Email").Select
Email = Cells(r, "F")
'       Message subject
Sheets("Email").Select
Subj = "Renewal for " & Cells(r, "B").Text & " Contract " & Cells(r, "A").Text & " Effective " & Cells(r, "C").Text

'       Message body
Sheets("Email").Select
strbody = strbody & "Dear " & Cells(r, "AR").Text & ", <br><br>" & _
"I will be working with you on " & Cells(r, "B") & ", client number " &       Cells(r, "A") & ", which is effective " & Cells(r, "C") & ".<br><br>" & _
"For this year's contract, we are requesting the following information: <br>" & _
"<li>" & Cells(r, "AH") & "</li>" & "<br><br>" & _
"The application form may be downloaded from:<br>" & _
"<li>Option #1</li>: " & "<a href=""" & "Link#1" & """>" &    "Link#1" & "</a>" & "<br>" & _
"<li>Option #2</li>: " & "<a href=""" & "link#2" & """>" & "link#2" & "</a>" & "<br><br>" & _
"Once we receive the requested information, you will receive your contract within 5 business days. Should you have any questions, please don't hesitate to contact me at this email address or phone number <br><br>" & _
"As always, we would like to thank you for your business. <br><br>" & _
"Regards, <br>"

On Error Resume Next

Sheets("Email").Select
With OMail
.Display
.To = Email
.Subject = Subj
.HTMLBody = strbody & vbNewLine & .HTMLBody
End With
Next r

On Error GoTo 0

Set OMail = Nothing
Set OApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:0)

请看一下这个例子。

在A栏:人民的名字 在B列:电子邮件地址 在C列中:Z:像这样的文件名C:\ Data \ Book2.xls(不必是Excel文件)

宏将遍历“Sheet1”中的每一行,如果B列中有电子邮件地址 和C列中的文件名:Z它将创建一个包含此信息的邮件并发送。

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

您可以从以下链接了解更多信息。

http://www.rondebruin.nl/win/s1/outlook/amail6.htm

答案 1 :(得分:-1)

如果您想使用此宏而不是邮件合并,那么您遇到的问题就在这里:

strbody = strbody & "Dear " & Cells(r, "AR").Text & ", <br><br>" & _
"I will be working with you on " & Cells(r, "B") & ", client number " &       Cells(r, "A") & ", which is effective " & Cells(r, "C") & ".<br><br>" & _
"For this year's contract, we are requesting the following information: <br>" & _
"<li>" & Cells(r, "AH") & "</li>" & "<br><br>" & _
"The application form may be downloaded from:<br>" & _
"<li>Option #1</li>: " & "<a href=""" & "Link#1" & """>" &    "Link#1" & "</a>" & "<br>" & _
"<li>Option #2</li>: " & "<a href=""" & "link#2" & """>" & "link#2" & "</a>" & "<br><br>" & _
"Once we receive the requested information, you will receive your contract within 5 business days. Should you have any questions, please don't hesitate to contact me at this email address or phone number <br><br>" & _
"As always, we would like to thank you for your business. <br><br>" & _
"Regards, <br>"

这将变量strbody和添加字符串的其余部分。

将其更新为:

strbody = "Dear " & Cells(r, "AR").Text & ", <br><br>" & _
"I will be working with you on " & Cells(r, "B") & ", client number " &       Cells(r, "A") & ", which is effective " & Cells(r, "C") & ".<br><br>" & _
"For this year's contract, we are requesting the following information: <br>" & _
"<li>" & Cells(r, "AH") & "</li>" & "<br><br>" & _
"The application form may be downloaded from:<br>" & _
"<li>Option #1</li>: " & "<a href=""" & "Link#1" & """>" &    "Link#1" & "</a>" & "<br>" & _
"<li>Option #2</li>: " & "<a href=""" & "link#2" & """>" & "link#2" & "</a>" & "<br><br>" & _
"Once we receive the requested information, you will receive your contract within 5 business days. Should you have any questions, please don't hesitate to contact me at this email address or phone number <br><br>" & _
"As always, we would like to thank you for your business. <br><br>" & _
"Regards, <br>"

每次都会覆盖它,这是我认为你想要的。

此外,您不需要每次都选择工作表(或者根本不需要)。选择工作表,单元格等通常是错误的编码实践,可能会显着降低代码速度。