首页 > 解决方案 > 如何遍历表格并将电子邮件发送给相应的用户

问题描述

我有一张桌子:

桌子

程序需要循环发送电子邮件到 NAV30 和 FED09。

Nav30 应该只接收表格的第 1 行和第 3 行。FED09 应该只接收第 2 行。

我找到了一个有效但不执行循环和抓取的代码

Sub Test2()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    Dim answer As String


    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")

    On Error GoTo cleanup
   answer = MsgBox("Are you sure you want to send these emails?", vbYesNo + vbQuestion)
   If answer = vbYes Then

    For Each cell In Columns("AC").Cells.SpecialCells(xlCellTypeConstants)
        If cell.Value Like "?*@?*.?*" And _
           LCase(Cells(cell.Row, "AD").Value) = "oui" _
           And LCase(Cells(cell.Row, "AE").Value) <> "sent" Then

            Set OutMail = OutApp.CreateItem(olMailItem)

            On Error Resume Next
            With OutMail
                .To = cell.Value
                .Subject = "blabla"
                .BodyFormat = olFormatHTML
                .HTMLBody = "blabla"
                               .Display
            End With
            On Error GoTo 0
            Cells(cell.Row, "AE").Value = "sent"
            Set OutMail = Nothing
        End If
    Next cell
    Else
    Exit Sub
    End If

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

标签: excelvbaoutlook

解决方案


推荐阅读