使用 VBA 向每个学生发送电子邮件的建议

Advice to send emails to each student using VBA

我正在尝试使用 VBA 向每个学生发送包含(学生姓名和他的分数)的电子邮件 ..

我有 excel sheet 如下

从上面excel我需要给每个学生发送电子邮件,电子邮件正文如下

Hi " Student name "

Below you can found your marks:-

Math :- " his mark"
Network :- "his mark"
Physics :- "his mark"
Antenna :- " his mark"

我已经在 VBA 中编写了代码,但我不知道如何将此文本发送给 mailBody 部分中的每个学生..

我的代码如下

Sub SendMail()
    Dim objEmail

    Const cdoSendUsingPort = 2  ' Send the message using SMTP
    Const cdoBasicAuth = 1      ' Clear-text authentication
    Const cdoTimeout = 100      ' Timeout for SMTP in seconds

     mailServer = "smtp.gmail.com"
     SMTPport = 465     '25 'SMTPport = 465
     mailusername = Range("j9").Value
     mailpassword = Range("j10").Value
     ''''''''
     Dim n As Integer
     n = Application.WorksheetFunction.CountA(Range("c:c")) - 1
     For i = 1 To n
     
     mailto = Range("c1").Offset(i, 0).Value
     mailSubject = Range("e1").Offset(i, 0).Value

     **mailBody = ??** What i should to set ?

    Set objEmail = CreateObject("CDO.Message")
    Set objConf = objEmail.Configuration
    Set objFlds = objConf.Fields

    With objFlds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = mailServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPport
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = cdoTimeout
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasicAuth
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = mailusername
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = mailpassword
    .Update
    End With

    objEmail.To = mailto
    objEmail.From = mailusername
    objEmail.subject = mailSubject
    objEmail.TextBody = mailBody
    'objEmail.AddAttachment "C:\report.pdf"
    objEmail.CC = Range("d1").Offset(i, 0).Value
    objEmail.BCC = Range("k1").Offset(i, 0).Value
    objEmail.Send

    Set objFlds = Nothing
    Set objConf = Nothing
    Set objEmail = Nothing
    Next i
End Sub

亲切的问候..

请试试这个方法:

 mailBody = "Hy " & Range("B" & i) & "," & vbCrLf & vbCrLf & _
           "Below you can find your marks:" & vbCrLf & vbCrLf & _
           "Network: - " & Range("G" & i) & vbCrLf & _
           "Physics: - " & Range("H" & i) & vbCrLf & _
           "Antenna: - " & Range("I" & i)

并从 2:

开始迭代
 For i = 2 To n

那就不需要任何 Offset:

objEmail.CC = Range("d" & i).Value
objEmail.BCC = Range("k" & i).Value