Vba 如何从代码发送电子邮件提醒 子发送提醒邮件() Dim OutlookApp作为对象 Dim OutLookMailItem作为对象 作为整数的Dim I计数器 将MailDest设置为字符串 设置OutlookApp=CreateObject(“Outlook.application”) 设置OutLookMailItem=OutlookApp.CreateItem(0) 使用OutLookMailItem MailDest=“” 对于iCounter=1的工作表function.CountA(列(34)) 如果MailDest=“”和单元格(iCounter,34).Offset(0,-1)=“发送提醒”,则 MailDest=单元格(iCounter,34).值 ElseIf MailDest“”和单元格(iCounter,34)。偏移量(0,-1)=“发送提醒”,然后 MailDest=MailDest&“;”&单元格(iCounter,34)。值 如果结束 下一个iCounter .BCC=MailDest .Subject=“ECR通知” .HTMLBody=“提醒:这是对自动ECR电子邮件通知的测试。请完成您的ECR任务” .发送 以 Set-OutLookMailItem=无 Set-OutlookApp=无 端接头
需要代码将AE列中的值通过电子邮件发送给“设置提醒”文本 GD mjac 你对你的信息还是很害羞 您呈现的代码收集所有地址,然后发送一条消息?我希望,根据您的示例表/数据,您希望向每个“打开”的ECR代码的每个收件人发送电子邮件 因此,假设如下:Vba 如何从代码发送电子邮件提醒 子发送提醒邮件() Dim OutlookApp作为对象 Dim OutLookMailItem作为对象 作为整数的Dim I计数器 将MailDest设置为字符串 设置OutlookApp=CreateObject(“Outlook.application”) 设置OutLookMailItem=OutlookApp.CreateItem(0) 使用OutLookMailItem MailDest=“” 对于iCounter=1的工作表function.CountA(列(34)) 如果MailDest=“”和单元格(iCounter,34).Offset(0,-1)=“发送提醒”,则 MailDest=单元格(iCounter,34).值 ElseIf MailDest“”和单元格(iCounter,34)。偏移量(0,-1)=“发送提醒”,然后 MailDest=MailDest&“;”&单元格(iCounter,34)。值 如果结束 下一个iCounter .BCC=MailDest .Subject=“ECR通知” .HTMLBody=“提醒:这是对自动ECR电子邮件通知的测试。请完成您的ECR任务” .发送 以 Set-OutLookMailItem=无 Set-OutlookApp=无 端接头,vba,excel,Vba,Excel,需要代码将AE列中的值通过电子邮件发送给“设置提醒”文本 GD mjac 你对你的信息还是很害羞 您呈现的代码收集所有地址,然后发送一条消息?我希望,根据您的示例表/数据,您希望向每个“打开”的ECR代码的每个收件人发送电子邮件 因此,假设如下: 您希望为“发送提醒”所在的每一行发送电子邮件 真的 “啊”列中的电子邮件地址每行都不同 在代码中,您使用Outlook.Application objectsSet-OutlookApp=CreateObject(“Outlook.Applicat
- 您希望为“发送提醒”所在的每一行发送电子邮件 真的
- “啊”列中的电子邮件地址每行都不同
Set-OutlookApp=CreateObject(“Outlook.Application”)
,请小心打开应用程序类型的对象,并确保在代码完成或触发错误时关闭它们,否则,您可能会得到大量使用有价值的资源“运行”的Outlook实例。下面的代码有一些基本的错误处理,以确保不再需要时关闭OutlookApp
对象
按如下方式设置工作簿:
在VB编辑器中的“工具|引用”下找到“Microsoft Outlook xx.x对象库”,其中xx.x表示您正在使用的Outlook版本。(另请参见:)这将有助于简化编码,因为您可以获得有关对象的intellisense建议
首先,将OutlookApp
声明为公共应用程序,包括所有其他子系统/功能等。
(即在“编码”窗口的顶部)
您的sendReminderMail()子系统
Public OutlookApp As Outlook.Application
增加了发送邮件功能:
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(senddress为字符串,ecr为长)为布尔值
'初始化函数返回值
sendMail=False
错误转到doEmailErr时:
'启动变量
将OutLookMailItem设置为Outlook.MailItem
Dim htmlBody作为字符串
'创建邮件项目
设置OutLookMailItem=OutlookApp.CreateItem(olMailItem)
'创建邮件的连接正文
htmlBody=“提醒:这是一个自动ECR电子邮件通知的测试。
”&_
“请完成ECR#”和CStr(ECR)的任务”
“Chuck”我在一起
使用OutLookMailItem
.BCC=发送地址
.Subject=“ECR通知”
.HTMLBody=HTMLBody
.发送
以
sendMail=True
doEmailErrExit:
退出功能
多梅勒:
MsgBox Err.Description、vbOKOnly、Err.Source&“:”和Err.Number
恢复doEmailErrExit
端函数
GD MJac,请更改您的问题,使代码实际显示为代码,并告知您迄今为止尝试过的内容,哪些不起作用,哪些有效。您是否收到错误消息。解释一下entrie代码示例应该做什么。你的问题没有包含足够的信息,所问的问题也不够精确。mtholen,这令人印象深刻。你的假设恰到好处。我很抱歉没有正确地表达我的问题,因为我不熟悉整个编程环境。我剩下的唯一问题是如何将ecr值的字体设置为粗体?如果使用语法.Font.Bold=TrueGD Mjac,最简单的方法是在“&Cstr(ecr)&”之前和之后添加。即“请完成ECR#”和CStr(ECR)的任务和“如果此答案回答了您的问题,请单击问题顶部投票箭头下的“接受”图标接受答案。
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