【问题标题】:Excel Macro To Send Specific Info In Multiple Separate EmailsExcel宏在多个单独的电子邮件中发送特定信息
【发布时间】:2016-05-31 19:38:10
【问题描述】:

所以我需要一些帮助。我正在尝试更新我公司用于管理客户的 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

【问题讨论】:

标签: excel vba email outlook


【解决方案1】:

请看一下这个例子。

在 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

【讨论】:

    【解决方案2】:

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

    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>"
    

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

    您也不需要每次都选择工作表(或者根本不需要)。选择工作表、单元格等通常是不好的编码习惯,会显着减慢您的代码速度。

    【讨论】:

    • 是的,这很好用!非常感谢您的帮助和建议!我是初学者,所以在我真正养成坏习惯之前,这些技巧非常有用。谢谢你这么有效率!
    • 不确定投反对票的目的是什么,因为尽管它没有被接受,但它似乎已经解决了 OP 的问题。
    猜你喜欢
    • 2010-10-14
    • 2019-06-19
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多