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
added 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
GD MJac,请更改您的问题,以便代码实际显示为代码并提供您迄今尝试过的内容,哪些内容不起作用。你收到错误消息吗?给出一个解释你的代码样本应该做什么。你的问题没有包含足够的信息,并且在问题中缺乏精确性。 – mtholen