Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 建议使用VBA向每位学生发送电子邮件_Excel_Vba_Vba7 - Fatal编程技术网

Excel 建议使用VBA向每位学生发送电子邮件

Excel 建议使用VBA向每位学生发送电子邮件,excel,vba,vba7,Excel,Vba,Vba7,我试图用VBA向每个学生发送包含(学生姓名和分数)的电子邮件 我有下面的excel表格 Hi " Student name " Below you can found your marks:- Math :- " his mark" Network :- "his mark" Physics :- "his mark" Antenna :- " his mark" 从上面的excel中,我

我试图用VBA向每个学生发送包含(学生姓名和分数)的电子邮件

我有下面的excel表格

Hi " Student name "

Below you can found your marks:-

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

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

Hi " Student name "

Below you can found your marks:-

Math :- " his mark"
Network :- "his mark"
Physics :- "his mark"
Antenna :- " his mark"
我已经用VBA编写了代码,但我不知道如何在邮件正文部分向每个学生发送这样的文本

我的代码如下

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
则无需进行任何
偏移

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

你的问题是什么?我的问题是如何构建邮件正文的结构,正如我在上面提到的,上面的结构只是一个字符串。如果正文格式为HTML,则在每行末尾添加br标记。使用字符串连接。在vba中,串联运算符是
&
。例如,
mailBody
(后面是一个空行)的第一行可以这样设置
mailBody=“Hello”&Range(“b1”)。偏移量(i,0)。值&“,”&Chr(10)&Chr(10)
@超对称,非常感谢您的支持。。(数学、网络等)是所有学生的固定列名,变量将是分数和学生名,,,,请你举个例子支持我,我在这方面是新手。我想你应该将
i
改为
i+1
@超级对称:在他写代码的方式中,是的。。。我还试着(在你的评论之后)建议他从
2
开始迭代。无论如何,谢谢你!你是对的。@Tarik:我的代码没有回答你的问题吗?如果现在,问题出在哪里?@FaneDuru,谢谢你的支持。。当我发送消息时,文本电子邮件正文仅显示“False”字样。。邮件正文中没有显示学生姓名或分数什么?字符串如何返回
False
?你可能做了一个错误的比较。。。只有这样,它才会返回“False”。。。