Programmatically Email Student Grades Individually

Andy0311

Board Regular
Joined
Oct 16, 2019
Messages
118
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Test TypeJohnJackBen ArtSam
Midterm 18075907095
Midterm 280100907095
Final Exam80951009095
Average8090937795

Hello - Above is a table I keep to track student grades. Its location is always A1:F5. Periodically, I email each student their personal scores (30-40 students versus the 5 I've show in the above example) using gmail. I would like to be able to programmatically email each student their set of grades. The body of the email would look like this:

Test Type John
Midterm 1 80
Midterm 2 80
Final Exam 80
Average 80

Any help achieving this would be appreciated. Thank you in advance.

Andy
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Andy

Do you want to send the email via Gmail?
 
Upvote 0
Do the following:
1. Before we get start, you need to add a reference to the CDO Library.
To do this use the Tools->References menu in the VBA-Editor
The full name of the library we are going to use is Microsoft CDO for Windows 2000.

2. Allow less secure apps to access your Gmail account

3. You did not indicate where to get the student's email account. In the macro I put an example for you to perform tests

4. Change "myEmail@gmail.com" and "myPassword" , for your data to send emails from Gmail.

5. Run this code:

VBA Code:
Sub Send_Gmail_to_students()
  Dim EmailMsg, EmailConf As Object, EmailFields As Variant
  Dim sh As Worksheet, Mess As String, Email As String
  Dim j As Long, EmailUsr As String, EmailPwd
  '
  EmailUsr = "myEmail@gmail.com"
  EmailPwd = "myPassword"
  Set sh = ActiveSheet
  For j = 2 To sh.Cells(1, Columns.Count).End(xlToLeft).Column
    Set EmailMsg = CreateObject("CDO.Message") 'CDO (Collaboration Data Objects) -Make sure you have the 'CDO For Windows' Library Selected
    Set EmailConf = CreateObject("CDO.Configuration")
    EmailConf.Load -1    ' Set CDO Source Defaults
    Set EmailFields = EmailConf.Fields
    With EmailFields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = EmailUsr
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = EmailPwd
      .Update
    End With
    Email = "student_1@yahoo.com"
    Mess = "<b>" & sh.Range("A1").Value & " " & sh.Cells(1, j).Value & "</b><br>" & _
                   sh.Range("A2").Value & " " & sh.Cells(2, j).Value & "<br>" & _
                   sh.Range("A3").Value & " " & sh.Cells(3, j).Value & "<br>" & _
                   sh.Range("A4").Value & " " & sh.Cells(4, j).Value & "<br>" & _
           "<b>" & sh.Range("A5").Value & " " & sh.Cells(5, j).Value & "</b>"
    With EmailMsg
      Set .Configuration = EmailConf
      .To = Email
      .CC = ""
      .From = EmailUsr
      .Subject = "Set of grades"
      .htmlBody = Mess
      On Error Resume Next
      .Send
      On Error GoTo 0
    End With
    If Err.Number = 0 Then
    Else
      MsgBox Err.Number & " " & Err.Description
    End If
    'Cleanup
    Set EmailMsg = Nothing
    Set EmailConf = Nothing
    Set EmailFields = Nothing
  Next
End Sub
 
Upvote 0
Andy

Do you want to send the email via Gmail?
Norie: I wrote to apologize for not replying. Your note got by me, and then I went on vacation and just got back. Best wishes and thanks again for your help. Andy
 
Upvote 0
Do the following:
1. Before we get start, you need to add a reference to the CDO Library.
To do this use the Tools->References menu in the VBA-Editor
The full name of the library we are going to use is Microsoft CDO for Windows 2000.

2. Allow less secure apps to access your Gmail account

3. You did not indicate where to get the student's email account. In the macro I put an example for you to perform tests

4. Change "myEmail@gmail.com" and "myPassword" , for your data to send emails from Gmail.

5. Run this code:

VBA Code:
Sub Send_Gmail_to_students()
  Dim EmailMsg, EmailConf As Object, EmailFields As Variant
  Dim sh As Worksheet, Mess As String, Email As String
  Dim j As Long, EmailUsr As String, EmailPwd
  '
  EmailUsr = "myEmail@gmail.com"
  EmailPwd = "myPassword"
  Set sh = ActiveSheet
  For j = 2 To sh.Cells(1, Columns.Count).End(xlToLeft).Column
    Set EmailMsg = CreateObject("CDO.Message") 'CDO (Collaboration Data Objects) -Make sure you have the 'CDO For Windows' Library Selected
    Set EmailConf = CreateObject("CDO.Configuration")
    EmailConf.Load -1    ' Set CDO Source Defaults
    Set EmailFields = EmailConf.Fields
    With EmailFields
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = EmailUsr
      .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = EmailPwd
      .Update
    End With
    Email = "student_1@yahoo.com"
    Mess = "<b>" & sh.Range("A1").Value & " " & sh.Cells(1, j).Value & "</b><br>" & _
                   sh.Range("A2").Value & " " & sh.Cells(2, j).Value & "<br>" & _
                   sh.Range("A3").Value & " " & sh.Cells(3, j).Value & "<br>" & _
                   sh.Range("A4").Value & " " & sh.Cells(4, j).Value & "<br>" & _
           "<b>" & sh.Range("A5").Value & " " & sh.Cells(5, j).Value & "</b>"
    With EmailMsg
      Set .Configuration = EmailConf
      .To = Email
      .CC = ""
      .From = EmailUsr
      .Subject = "Set of grades"
      .htmlBody = Mess
      On Error Resume Next
      .Send
      On Error GoTo 0
    End With
    If Err.Number = 0 Then
    Else
      MsgBox Err.Number & " " & Err.Description
    End If
    'Cleanup
    Set EmailMsg = Nothing
    Set EmailConf = Nothing
    Set EmailFields = Nothing
  Next
End Sub

Please forgive me sir, for not replying to you until now. Your message just got by me, and then I went on vacation, and just got back. I appreciate the help and code, and will run it and get back on the results. Again, thank you and accept my apologies for not replying sooner. Andy
 
Upvote 0
Don't worry, let me know if you have any doubts.
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,090
Members
449,065
Latest member
Danger_SF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top