如果单元格的值为1,则向Outlook电子邮件添加电子邮件地址

时间:2015-11-18 22:43:52

标签: excel vba email outlook

我正在尝试向E列中发送“1”的任何人发送电子邮件。我有一份工作表,其中包含一系列员工及其电话号码。在他们的电话号码之后是他们想要通知的事件列表。如果我在其名称后面的一列中放置1,我希望将他们的电子邮件地址添加到电子邮件中。

列标题类似于A =姓氏,B =名字,C =电话号码,D =电子邮件地址,E =事件1,F =事件2,G =事件3,依此类推。

我能够获取电子邮件代码以从设定范围中提取所有电子邮件地址,但我无法弄清楚如何添加if命令以在其中一个事件列中查找设定值(E,F, G...)。它需要遍历整个范围,然后仅为那些在其中一个事件列中具有设定值的所选个人提取地址。

到目前为止,这是我的代码:

Dim OutApp As Object
Dim OutMail As Object
Dim strto As String, strcc As String, strbcc As String
Dim strsub As String, strbody As String
Dim emailRng As Range, cl As Range
Dim sTo As String

Set emailRng = Worksheets("Sheet1").Range("D1:D500")

For Each cl In emailRng
    sTo = sTo & ";" & cl.Value
Next

sTo = Mid(sTo, 2)


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


strto = sTo

strcc = ""
strbcc = ""
strsub = "'NOTIFICATION'  - " & "Notification"
strbody = "<img src=Z:\Logo2.jpg width=624 height=74>" & _
          "<font size=2 font face=Verdana color=black>" & "<br>" & _
          "The following notification was received:</B><br><br>" & _
          "COPY AND PAST NOTIFICATION RECEIVED HERE" & "<br><br>" & _
          "<b>Control Center</b><br><br>"


With OutMail
    '.SentOnBehalfOfName = ""
    .to = strto
    .cc = strcc
    .bcc = strbcc
    .Subject = strsub
    'You can add a file to the mail like this
    .HTMLBody = strbody
    .Display    ' or use .Send
End With

Set OutMail = Nothing
Set OutApp = Nothing

1 个答案:

答案 0 :(得分:0)

基本答案是:

For Each cl In emailRng
    If cl.Offset(,1) = 1 Then sTo = sTo & ";" & cl.Value
Next

在此检查是否检查了电子邮件地址(列表E中的事件1)1列以进行通知。

但是,由于您声明可能有 n 个事件,因此您需要一种方法来确定要为其发送电子邮件的事件。我不知道你打算如何处理它,但你可以,例如,从另一个Sub调用Sub并传递事件编号并将其用作偏移量。

所以

Sub EmailEvent()

    SendEventEmail 1 'send event 1 email
    SendEventEmail 2 'send event 2 email
    SendEventEmail 3 'send event 3 email

End Sub

然后在实际发送邮件的子邮件中,它会说

Sub SendEventEmail(n as Long)
Rem n is the Event Number

Rem Your Code ... 

For Each cl in emailRng
     If cl.Offset(,n) = 1 Then sTo = sTo & ";" & cl.Value
Next

Rem Rest of code ... 

End Sub