【问题标题】:How to send email reminder from code如何从代码发送电子邮件提醒
【发布时间】:2015-10-18 13:36:53
【问题描述】:
Sub SendReminderMail()
  Dim OutlookApp As Object
  Dim OutLookMailItem As Object
  Dim iCounter As Integer
  Dim MailDest As String

  Set OutlookApp = CreateObject("Outlook.application")
  Set OutLookMailItem = OutlookApp.CreateItem(0)

  With OutLookMailItem
    MailDest = ""

    For iCounter = 1 To WorksheetFunction.CountA(Columns(34))
      If MailDest = "" And Cells(iCounter, 34).Offset(0, -1) = "Send Reminder" Then
        MailDest = Cells(iCounter, 34).Value
      ElseIf MailDest <> "" And Cells(iCounter, 34).Offset(0, -1) = "Send Reminder" Then
        MailDest = MailDest & ";" & Cells(iCounter, 34).Value
      End If
    Next iCounter

    .BCC = MailDest
    .Subject = "ECR Notification"
    .HTMLBody = "Reminder: This is a test for an automatic ECR email notification. Please complete your tasks for ECR#"
    .Send
  End With

  Set OutLookMailItem = Nothing
  Set OutlookApp = Nothing
End Sub

需要代码通过“设置提醒”文本通过电子邮件发送 AE 列中的值

【问题讨论】:

  • GD MJac,请更改您的问题,以便代码实际显示为代码,并告知您迄今为止尝试过的内容,无效的内容。您是否收到错误消息。解释您的条目代码示例应该做什么。您的问题没有包含足够的信息,并且在提出的问题中缺乏精确性。

标签: vba excel


【解决方案1】:

GD mjac,

你仍然对你的信息感到害羞......?

您提供的代码收集所有地址并随后发送一条消息?我希望,根据您的示例表/数据,您希望针对每个“开放”的 ECR 代码向每个收件人发送电子邮件?

所以假设如下:

  • 您想为“发送提醒”所在的每一行发送一封电子邮件 是的
  • 每一行的“AH”列中的电子邮件地址都不同?

在您的代码中,您使用 Outlook.Application 对象 Set OutlookApp = CreateObject("Outlook.application"),打开应用程序类型对象时要小心,并确保在代码完成或触发错误时将其关闭,否则您可能会最终会出现许多使用有价值的资源“运行”的 Outlook 实例。下面的代码有一些基本的错误处理,以确保 OutlookApp 对象在不再需要时关闭。

如下设置您的工作簿:

在工具|参考下的 VB 编辑器中找到“Microsoft Outlook xx.x 对象库”,其中 xx.x 表示您正在使用的 Outlook 版本。 (另请参阅:https://msdn.microsoft.com/en-us/library/office/ff865816.aspx)这将使您在获得对象的智能感知建议时更容易编码。

OutlookApp 声明为公共,高于所有其他子/功能等。 (即在“编码”窗口的顶部)

Public OutlookApp As Outlook.Application

您的 sendReminderMail() 子

Sub SendReminderMail()
  Dim iCounter As Integer
  Dim MailDest As String
  Dim ecr As Long

    On Error GoTo doOutlookErr:
    Set OutlookApp = New Outlook.Application

    For iCounter = 1 To WorksheetFunction.CountA(Columns(34))
        MailDest = Cells(iCounter, 34).Value
        ecr = Cells(iCounter, 34).Offset(0, -3).Value

        If Not MailDest = vbNullString And Cells(iCounter, 34).Offset(0, -1) = "Send Reminder" Then
          sendMail MailDest, ecr
          MailDest = vbNullString
        End If

    Next iCounter

'basic errorhandling to prevent Outlook instances to remain open in case of an error.
doOutlookErrExit:
    If Not OutlookApp Is Nothing Then
        OutlookApp.Quit
    End If
    Exit Sub

doOutlookErr:
    MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
    Resume doOutlookErrExit

End Sub

新增发送邮件功能:

Function sendMail(sendAddress As String, ecr As Long) As Boolean

    'Initiate function return value
    sendMail = False
    On Error GoTo doEmailErr:

    'Initiate variables
    Dim OutLookMailItem As Outlook.MailItem
    Dim htmlBody As String

    'Create the mail item
    Set OutLookMailItem = OutlookApp.CreateItem(olMailItem)

    'Create the concatenated body of the mail
    htmlBody = "<html><body>Reminder: This is a test for an automatic ECR email notification.<br>" & _
                "Please complete your tasks for ECR#" & CStr(ecr) & "</body></html>"

    'Chuck 'm together and send
    With OutLookMailItem

        .BCC = sendAddress
        .Subject = "ECR Notification"
        .HTMLBody = htmlBody
        .Send

    End With

    sendMail = True

doEmailErrExit:
    Exit Function

doEmailErr:
    MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
    Resume doEmailErrExit


End Function

【讨论】:

  • mtholen,这令人印象深刻。你的假设是正确的。我很抱歉没有正确传达我的问题,因为我是整个编程环境的新手。我剩下的唯一问题是如何使 ecr 值的字体加粗?是否使用语法 .Font.Bold=True
  • GD Mjac,最简单的方法是在 "& Cstr(ecr) &" 之前添加 并在其后添加 。 IE。 "请完成 ECR#" & CStr(ecr) & "
猜你喜欢
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2014-09-27
  • 1970-01-01
相关资源
最近更新 更多