Excel 根据单元格值选择不同的电子邮件正文

Excel 根据单元格值选择不同的电子邮件正文,excel,vba,outlook,Excel,Vba,Outlook,根据D列中的值,将拾取3个正文内容 1) 如果“D”列值为“高”,则应选择bodycontent1 2) 如果“D”列值为“中”,则应选择bodycontent2 3) 如果“D”列值为“低”,则应选择bodycontent3 下面的代码只是为任何条件选择bodycontent1 代码: Option Explicit Public Sub Example() Dim olApp As Outlook.Application Dim olNs As Outlook.Namespace Dim I

根据D列中的值,将拾取3个正文内容

1) 如果“D”列值为“高”,则应选择bodycontent1

2) 如果“D”列值为“中”,则应选择bodycontent2

3) 如果“D”列值为“低”,则应选择bodycontent3

下面的代码只是为任何条件选择bodycontent1

代码:

Option Explicit
Public Sub Example()
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Inbox As Outlook.MAPIFolder
Dim Item As Variant
Dim MsgFwd As MailItem
Dim Items As Outlook.Items
Dim Email As String
Dim Email1 As String
Dim ItemSubject As String
Dim lngCount As Long
Dim i As Long
Dim RecipTo As Recipient
Dim RecipCC As Recipient
Dim RecipBCC As Recipient
Dim onbehalf As Variant
Dim EmailBody As String
Dim BodyName As String
Dim Bodycontent1 As String
Dim Bodycontent2 As String
Dim Bodycontent3 As String
Dim Criteria1 As String


Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Set Items = Inbox.Items

i = 2 '  i = Row 2

With Worksheets("Sheet1") ' Sheet Name
Do Until IsEmpty(.Cells(i, 1))

ItemSubject = .Cells(i, 1).Value '(i, 1) = (Row 2,Column 1)
Email = .Cells(i, 16).Value '(i, 2) = (Row 2,Column 2)
Email1 = .Cells(i, 2).Value
Criteria1 = .Cells(i, 4).Value

Bodycontent1 = "Hello this is for testing purpose1" & "<BR>" & _
"Regards," & "<BR>" & _
"Kelvin"

Bodycontent2 = "Hello this is for testing purpose2" & "<BR>" & _
"Regards," & "<BR>" & _
"Kelvin"

Bodycontent3 = "Hello this is for testing purpose3" & "<BR>" & _
"Regards," & "<BR>" & _
"Kelvin"


 '// Loop through Inbox Items backwards
 For lngCount = Items.Count To 1 Step -1
 Set Item = Items.Item(lngCount)

 If Item.Subject = ItemSubject Then ' if Subject found then
 Set MsgFwd = Item.Forward




Set RecipTo = MsgFwd.Recipients.Add(Email1) 
Set RecipTo = MsgFwd.Recipients.Add("secnww@hp.com")
Set RecipBCC = MsgFwd.Recipients.Add(Email) 
MsgFwd.SentOnBehalfOfName = "doc@hp.com"
BodyName = .Cells(i, 3).Value

RecipTo.Type = olTo
RecipBCC.Type = olBCC

Debug.Print Item.Body

If Criteria1 = "high" Then

MsgFwd.HTMLBody = Bodycontent1 & Item.HTMLBody

ElseIf Criteria1 = "medium" Then

MsgFwd.HTMLBody = Bodycontent2 & Item.HTMLBody

Else 'If Criteria1 = "Low" Then

MsgFwd.HTMLBody = Bodycontent3 & Item.HTMLBody

MsgFwd.Display

End If
End If



Next ' exit loop

i = i + 1 '  = Row 2 + 1 = Row 3
Loop
End With

Set olApp = Nothing
Set olNs = Nothing
Set Inbox = Nothing
Set Item = Nothing
Set MsgFwd = Nothing
Set Items = Nothing

MsgBox "Mail sent"

End Sub
选项显式
公共子示例()
Dim olApp作为Outlook.Application
将OLN设置为Outlook.Namespace
将收件箱暗显为Outlook.Mapi文件夹
作为变体的Dim项目
将MsgFwd设置为邮件项
将项目设置为Outlook。项目
将电子邮件设置为字符串
将Email1设置为字符串
将项目主题设置为字符串
暗计数等于长
我想我会坚持多久
收件人
将抄送作为收件人
将密件抄送为收件人
代表作为变体
将电子邮件正文设置为字符串
将BodyName设置为字符串
将Bodycontent1设置为字符串
暗淡的Bodycontent2作为字符串
暗淡的Bodycontent3作为字符串
Dim Criteria1作为字符串
设置olApp=CreateObject(“Outlook.Application”)
Set olNs=olApp.GetNamespace(“MAPI”)
设置收件箱=olNs.GetDefaultFolder(olFolderInbox)
设置项目=收件箱。项目
i=2'i=2排
带有工作表(“表1”)的工作表名称
直到空(.Cells(i,1))为止
ItemSubject=.Cells(i,1).Value'(i,1)=(第2行,第1列)
电子邮件=.Cells(i,16).Value'(i,2)=(第2行,第2列)
Email1=.Cells(i,2).Value
准则1=.Cells(i,4).Value
Bodycontent1=“您好,这是用于测试目的1”&“
”和_ “问候,&”
“&”_ “开尔文” Bodycontent2=“您好,这是为了测试目的2”&“
”和_ “问候,&”
“&”_ “开尔文” Bodycontent3=“您好,这是为了测试目的3”&“
”和_ “问候,&”
“&”_ “开尔文” “//向后循环收件箱中的项目 对于lngCount=项目。计数到1步骤-1 设置项目=项目。项目(lngCount) 如果Item.Subject=ItemSubject,则“如果找到主题,则 设置MsgFwd=Item.Forward 设置RecipTo=MsgFwd.Recipients.Add(Email1) 设置RecipTo=MsgFwd.Recipients.Add(“secnww@hp.com") 设置recipbc=MsgFwd.Recipients.Add(电子邮件) MsgFwd.sentonBehalfName=”doc@hp.com" BodyName=.Cells(i,3).Value RecipTo.Type=olTo RecipBCC.Type=olBCC Debug.Print Item.Body 如果标准1=“高”,则 MsgFwd.HTMLBody=Bodycontent1和Item.HTMLBody ElseIf Criteria1=“中等”然后 MsgFwd.HTMLBody=Bodycontent2和Item.HTMLBody 否则“如果标准1=”低“,则 MsgFwd.HTMLBody=Bodycontent3和Item.HTMLBody MsgFwd.Display 如果结束 如果结束 下一个“退出循环” i=i+1'=第2行+1=第3行 环 以 设置olApp=Nothing 设置olNs=Nothing 设置收件箱=无 设置项=无 设置MsgFwd=Nothing 设置项=无 MsgBox“已发送邮件” 端接头
  • 您应该使用
    Select Case
    而不是
    If/ElseIf
  • 请参阅关于LastRow的部分,该部分比Loop+
    i=i+1更清晰
  • 我已经为(注释)添加了一个退出,以防您想赢得时间,并且只转发第一条包含您要查找的主题的消息 最终代码:

    Option Explicit
    Public Sub Example()
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim Item As Variant
    Dim MsgFwd As MailItem
    Dim wS As Worksheet
    Dim Items As Outlook.Items
    Dim Email As String
    Dim Email1 As String
    Dim ItemSubject As String
    Dim lngCount As Long
    Dim LastRow As Long
    Dim i As Long
    Dim BodyName As String
    Dim Bodycontent1 As String
    Dim Bodycontent2 As String
    Dim Bodycontent3 As String
    Dim Criteria1 As String
    
    
    Set olApp = CreateObject("Outlook.Application")
    Set olNs = olApp.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set Items = Inbox.Items
    
    
    Bodycontent1 = "Hello this is for testing purpose1" & "<BR>" & _
    "Regards," & "<BR>" & _
    "Kelvin"
    
    Bodycontent2 = "Hello this is for testing purpose2" & "<BR>" & _
    "Regards," & "<BR>" & _
    "Kelvin"
    
    Bodycontent3 = "Hello this is for testing purpose3" & "<BR>" & _
    "Regards," & "<BR>" & _
    "Kelvin"
    
    
    
    Set wS = thisworkbook.Worksheets("Sheet1") ' Sheet Name
    With wS
        LastRow = .Range("A" & .rows.Count).End(xlup).Row
        For i = 2 To LastRow
            ItemSubject = .Cells(i, 1).value
            Email = .Cells(i, 16).value
            Email1 = .Cells(i, 2).value
            Criteria1 = .Cells(i, 4).value
            BodyName = .Cells(i, 3).value
    
            '// Loop through Inbox Items backwards
            For lngCount = Items.Count To 1 Step -1
                Set Item = Items.Item(lngCount)
    
                If Item.Subject <> ItemSubject Then
                Else
                    'If Subject found then
                    Set MsgFwd = Item.Forward
                    With MsgFwd
                        .To = Email1 & " ; secnww@hp.com"
                        .BCC = Email
                        .SentOnBehalfOfName = "doc@hp.com"
    
                        Select Case LCase(Criteria1)
                            Case Is = "high"
                                .HTMLBody = Bodycontent1 & Item.HTMLBody
                            Case Is = "medium"
                                .HTMLBody = Bodycontent2 & Item.HTMLBody
                            Case Is = "low"
                                .HTMLBody = Bodycontent3 & Item.HTMLBody
                            Case Else
                                MsgBox "Criteria : " & Criteria1 & " not recognised!", _
                                        vbCritical + vbOKOnly, "Case not handled"
                        End Select
    
                        .Display
                        'Exit For
                    End With 'MsgFwd
                End If
            Next lngCount
        Next i
    End With 'wS
    
    Set olApp = Nothing
    Set olNs = Nothing
    Set Inbox = Nothing
    Set Item = Nothing
    Set MsgFwd = Nothing
    Set Items = Nothing
    
    MsgBox "Mail sent"
    
    End Sub
    
    选项显式
    公共子示例()
    Dim olApp作为Outlook.Application
    将olNs设置为Outlook.NameSpace
    将收件箱暗显为Outlook.Mapi文件夹
    作为变体的Dim项目
    将MsgFwd设置为邮件项
    将wS设置为工作表
    将项目设置为Outlook。项目
    将电子邮件设置为字符串
    将Email1设置为字符串
    将项目主题设置为字符串
    暗计数等于长
    最后一排一样长
    我想我会坚持多久
    将BodyName设置为字符串
    将Bodycontent1设置为字符串
    暗淡的Bodycontent2作为字符串
    暗淡的Bodycontent3作为字符串
    Dim Criteria1作为字符串
    设置olApp=CreateObject(“Outlook.Application”)
    Set olNs=olApp.GetNamespace(“MAPI”)
    设置收件箱=olNs.GetDefaultFolder(olFolderInbox)
    设置项目=收件箱。项目
    Bodycontent1=“您好,这是用于测试目的1”&“
    ”和_ “问候,&”
    “&”_ “开尔文” Bodycontent2=“您好,这是为了测试目的2”&“
    ”和_ “问候,&”
    “&”_ “开尔文” Bodycontent3=“您好,这是为了测试目的3”&“
    ”和_ “问候,&”
    “&”_ “开尔文” 设置wS=thisworkbook.Worksheets(“Sheet1”)的工作表名称 与wS LastRow=.Range(“A”&.rows.Count).End(xlup).Row 对于i=2到最后一行 ItemSubject=.Cells(i,1).value 电子邮件=.Cells(i,16).value Email1=.Cells(i,2).value 准则1=.Cells(i,4).value BodyName=.Cells(i,3).value “//向后循环收件箱中的项目 对于lngCount=项目。计数到1步骤-1 设置项目=项目。项目(lngCount) 如果Item.Subject ItemSubject,则 其他的 “如果找到了目标 设置MsgFwd=Item.Forward 使用MsgFwd .To=电子邮件1&“;secnww@hp.com" .BCC=电子邮件 .SentonBehalfName=”doc@hp.com" 选择案例LCase(标准1) Case Is=“高” .HTMLBody=Bodycontent1和Item.HTMLBody Case Is=“中等” .HTMLBody=Bodycontent2和Item.HTMLBody Case Is=“低” .HTMLBody=Bodycontent3和Item.HTMLBody 其他情况 MsgBox“标准:&Criteria1&“未识别!”_ vbCritical+vbOKOnly,“未处理案例” 结束选择 .展示 "退出 以“MsgFwd”结尾 如果结束 下一个lngCount 接下来我 以“wS”结尾 设置olApp=Nothing 设置olNs=Nothing 设置收件箱=无 设置项=无 设置MsgFwd=Nothing 设置项=无 MsgBox“已发送邮件” 端接头
    感谢它的作品。。但当我试图将标准从高、低和中更改为净化、非净化和APJ时。。它不起作用。。您能帮我理解一下吗。@开尔文:您是否同时更改了excel和代码中的值?您是否注意到
    Select Case LCase(Criteria1)
    中的
    LCase
    ?一切都会好起来的