1

我之前对其他类型的邮件使用了以下编码,但这次不发送邮件。

也不选择 Excel 中给出的电子邮件 ID 和数据。
我必须通过 CC 向不同的人发送邮件给他们各自的经理。我在 Excel 中更新了他们的电子邮件 ID,但在 Excel 中更新的详细信息没有被使用,并且在 To 和 CC 中没有任何内容的情况下创建了草稿邮件。

Sub Send_Recertification_From_Excel()

Dim oXlWkBk As Excel.Workbook ' Excel Work Book Object
Dim oOLApp As Outlook.Application
Dim oOLMail As MailItem
Dim lRow As Long
Dim olMailItem
Dim sMailID As String
Dim sSalutation As String
Dim sName As String
Dim sDetails As String
Dim sSubject As String
Dim mailsSentString As String
Dim templateName As String
templateName = "C:\Users\m540797\Desktop\Recertification\Recertifications"
On Error GoTo Err_Trap

Set oXlWkBk = ActiveWorkbook
Set oOLApp = GetObject(, "Outlook.Application")
If oOLApp Is Nothing Then
    MsgBox "Please Open Outlook.."
    Exit Sub
End If

Dim i As Integer

For i = 6 To 50

    If Len(Trim(Sheet1.Cells(i, 1))) > 1 Then
        Set oOLMail = oOLApp.CreateItemFromTemplate(templateName)
        sMailID = Sheet1.Cells(i, 4)
        sSubject = "Recertification"
        
        With oOLMail
            .BodyFormat = olFormatHTML
            .HTMLBody = Replace(.HTMLBody, "<NAME>", Sheet1.Cells(i, 3))
            .SentOnBehalfOfName = "my mail ID given here"
            .To = sMailID
            .Subject = sSubject
            .CC = Sheet1.Cells(i, 6)
            .Send
        End With
    
        oOLMail.Send
    
    Else
        Exit For
    End If

Next i
MsgBox "Mails successfully sent to :" + vbCrLf + mailsSentString + vbCrLf + "with using the template :" + templateName

Destroy_Objects:
If Not oOLApp Is Nothing Then Set oOLApp = Nothing

Err_Trap:
    If Err <> 0 Then
        MsgBox Err.Description, vbInformation, "VBADUD AutoMail"
        Err.Clear
        GoTo Destroy_Objects
    End If

End Sub

没有给出任何错误只是说邮件发送成功,但它正在草稿中。

4

0 回答 0