Excel 根据电子邮件正文中的唯一值和分组行发送电子邮件

Excel 根据电子邮件正文中的唯一值和分组行发送电子邮件,excel,vba,email,outlook,Excel,Vba,Email,Outlook,我希望在“比较”列需要大于4时发送电子邮件,但我希望根据唯一的“公司ID”对电子邮件进行分组。在邮件正文中,我希望它显示与该公司ID关联的所有行,其中“比较”大于4 Excel示例剪贴画 例如,在上面的snip中,我想向公司ID 12345、19876和45678发送一封电子邮件,因为它们在“比较”列中的行数高于4。我希望电子邮件对每个唯一公司ID的行进行分组(并且仅对“比较”>4的行进行分组),但我希望每个唯一值填充一封单独的电子邮件。像这样: 电子邮件示例 我获得了正确生成电子邮件的代码

我希望在“比较”列需要大于4时发送电子邮件,但我希望根据唯一的“公司ID”对电子邮件进行分组。在邮件正文中,我希望它显示与该公司ID关联的所有行,其中“比较”大于4

Excel示例剪贴画

例如,在上面的snip中,我想向公司ID 12345、19876和45678发送一封电子邮件,因为它们在“比较”列中的行数高于4。我希望电子邮件对每个唯一公司ID的行进行分组(并且仅对“比较”>4的行进行分组),但我希望每个唯一值填充一封单独的电子邮件。像这样:

电子邮件示例

我获得了正确生成电子邮件的代码,但我不知道如何让电子邮件正文执行我希望它执行的操作:

Sub EmailDivisions()

Dim cell, lookrng As Range
Dim strDir As String
Dim strFilename As String
Dim sigString As String
Dim strBody As String
Dim strName As Variant
Dim strName1 As Variant
Dim strDept As Variant
Dim strName2 As String
Dim strName3 As Variant
Dim OlApp As New Outlook.Application
Dim myNameSp As Outlook.Namespace
Dim myInbox As Outlook.MAPIFolder
Dim myExplorer As Outlook.Explorer
Dim NewMail As Outlook.MailItem
Dim OutOpen As Boolean


    Set WS = Worksheets("Output")
    
    Application.ScreenUpdating = False
    
    OutOpen = True
    Set myExplorer = OlApp.ActiveExplorer
    If TypeName(myExplorer) = "Nothing" Then
        OutOpen = False
        Set myNameSp = OlApp.GetNamespace("MAPI")
        Set myInbox = myNameSp.GetDefaultFolder(olFolderInbox)
        Set myExplorer = myInbox.GetExplorer
    End If


Set rng = ActiveSheet.UsedRange

R = 2

Do While R <= rng.Rows.Count
Debug.Print LCase(rng.Cells(R, 3))
If Cells(R, 1).Value > 0 And Cells(R, 10).Value > 4 And Cells(R, 10).Value <> "No Previous Record 
Found" Then

    Set NewMail = OlApp.CreateItem(olMailItem)
    ElseIf Cells(R, 1).Value > 0 And Cells(R, 10).Value <= 4 Then GoTo ContinueLoop

    End If

Set strCompanyID = Cells(R, 3)
Set strAccountNumber = Cells(R, 4)
Set strCompanyName = Cells(R, 5)
Set strIndvName = Cells(R, 6)
Set strCorrections = Cells(R, 8)
Set strCompare = Cells(R, 10)
strName2 = Left(strAccountNumber, InStr(strAccountNumber & " ", " ") - 1)

With NewMail
.SentOnBehalfOfName = ""
.To = ""
.Subject = strCompanyID
.HTMLBody = "<Font Face=calibri>" & "Hello, <br><br>" & strName2

NewMail.Display

'See if the next row is for the same sender.  If so, process that
'row as well.  And then keep doing it until no more rows match
Do While rng.Cells(R, 3).Value = rng.Cells(R + 1, 3)
    R = R + 1
    Set strDept = Cells(R, 4)

ContinueLoop:
    Loop


Set NewMail = Nothing
R = R + 1
End With
Loop
Set OlApp = Nothing

On Error Resume Next


End Sub
子分区()
暗淡单元格,看起来像范围
作为字符串的Dim strDir
将strFilename设置为字符串
字符串作为字符串
像弦一样暗的链子
Dim strName作为变体
Dim-strName1作为变异体
作为变体的Dim strDept
将strName2设置为字符串
Dim-strName3作为变异体
Dim OlApp作为新的Outlook.Application
将myNameSp设置为Outlook.Namespace
将myInbox暗显为Outlook.Mapi文件夹
将myExplorer设置为Outlook.Explorer
将NewMail设置为Outlook.MailItem
Dim OutOpen为布尔型
设置WS=工作表(“输出”)
Application.ScreenUpdating=False
OutOpen=True
设置myExplorer=OlApp.ActiveExplorer
如果TypeName(myExplorer)=“Nothing”,则
OutOpen=False
设置myNameSp=OlApp.GetNamespace(“MAPI”)
设置myInbox=myNameSp.GetDefaultFolder(olFolderInbox)
设置myExplorer=myInbox.GetExplorer
如果结束
设置rng=ActiveSheet.UsedRange
R=2
当r0和单元格(R,10)时执行。值>4和单元格(R,10)。值“无以前的记录
找到“那么
Set NewMail=OlApp.CreateItem(olMailItem)
ElseIf单元格(R,1).值>0和单元格(R,10).值