[英]From Excel - Send Email Reminder When Criteria is met using VBA Code
[英]How to send email reminder from code
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,
你還是對你的信息害羞......?
您提供的代碼收集所有地址並隨后發送一條消息? 我希望,根據您的示例工作表/數據,您希望為每個“打開”的 ECR 代碼向每個收件人發送電子郵件?
所以假設如下:
在您的代碼中,您使用 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
添加了 sendMail 功能:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.