在VBA中发送电子邮件时出现自动化错误

时间:2019-05-17 12:02:16

标签: excel vba outlook outlook-vba

enter image description here

我正在根据特定条件通过VBA发送电子邮件。 该代码在第一次迭代中工作正常,当控件传递到第二次迭代时,我遇到“自动化错误”,有人可以帮我解决此问题。

    Dim Tbl As ListObject
    Dim LastRow2  As Long, LastRow3 As Long
    Dim i As Integer
    Dim DevName As String
    Dim USDescription As String
    Dim USnum As String

    Set Tbl = wb.Worksheets("Q2.5").ListObjects("Table4")
    MainSheet.Activate
    LastRow3 = Tbl.ListColumns(1).Range.Rows.Count '<-- last row in Column A in your Table

    i = 1
    Do Until i = LastRow3
        MainSheet.Activate

        If MainSheet.Range("A" & i).Interior.Color = RGB(193, 193, 193) Then
            'MsgBox "A" & i & "  is red!!"
            DevName = MainSheet.Range("A" & i).Offset(0, 4).Value
            USnum = MainSheet.Range("A" & i).Offset(0, 0).Value
            USDescription = MainSheet.Range("A" & i).Offset(0, 3).Value

            Dim OlApp As Outlook.Application
            Set OlApp = CreateObject("Outlook.Application")

            Dim OlMail As Outlook.MailItem
            Set OlMail = OlApp.CreateItem(OlMailItem)

            If DevName = "Nikith" Then
                OlMail.To = "karthik.ba@gmail.com" --> **'Getting error here on the 2nd iteration**
                OlMail.CC = "harish.joei@gmail.com"[![enter image description here][1]][1]
                OlMail.Subject = "Effort Overdue for the user story" & USnum & "-" & USDescription
                OlMail.Body = "Effort Overdue for the user story" & USnum & "-" & USDescription
                OlMail.Send
            ElseIf DevName = "Sayed" Then
                OlMail.To = "sayed.raheman@gmail.com"
                OlMail.CC = "harish.joe@gmail.com"
                OlMail.Subject = "Effort Overdue for the user story" & USnum & "-" & USDescription
                OlMail.Body = "Effort Overdue for the user story" & USnum & "-" & USDescription
                OlMail.Send
            End If
        End If
        i = i + 1
    Loop
End Sub

0 个答案:

没有答案