【问题标题】:Choose different email body based on cell value根据单元格值选择不同的电子邮件正文
【发布时间】:2017-03-17 08:03:20
【问题描述】:

根据D列的值选取3个body内容。

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

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

3) 如果“D”列值为“Low”,则应选择 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

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:
    1. 您应该使用Select Case 而不是If/ElseIf
    2. 看LastRow部分比Loop+i=i+1清晰
    3. 我添加了一个Exit For(已评论),以防您想赢得时间,并且只转发您正在寻找的主题的第一条消息!

    最终代码:

    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
    

    【讨论】:

    • 感谢它的工作.. 但是当我尝试将标准从高、低和中更改为清除、非清除和 APJ 时.. 它不起作用.. 你能帮我理解一下吗.
    • @Kelvin :您是否更改了 excel 和代码中的值?你注意到Select Case LCase(Criteria1) 中的LCase了吗?它会将所有字母设置为小写,因此您下面的选项也必须小写,即 Excel 中的 APJ 和代码中的 apj
    • 啊,我的错。我在 excel 中将其输入为 Purge,在 VBA 中输入为 purge。但那是另一个挑战,如何将其设置为 Purge、Non-Purge 和 APJ?
    • @Kelvin :您可以在 Excel 中使用大写字母,但在代码中,如果您让 LCase(),则它必须是小写字母。如果您希望测试区分大小写,可以将其删除。因此,根据您的新条件:您可以在 Excel 中输入 PurgeNon-PurgeAPJ,如果您输入 LCase,则它必须是 purgenon-purgeapj。顺便说一句,Select 案例结构的最大优势是您可以轻松添加新的测试可能性! ;)
    • @Kelvin :很高兴我能帮上忙! ;)
    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多