VBA not error in mail merge macro. No Recipient

robgoldstein

Board Regular
Joined
Oct 26, 2013
Messages
165
Office Version
  1. 2019
Platform
  1. Windows
I am trying to send a mail merge using VBA. I based it on code I got from this tutorial https://www.youtube.com/watch?v=WoCuRFEuACg&t=289s and then made some changes.

When I tested it after changing the subject info, but not the message yet I got an error saying At least one Recipient is required, but none were found.

Here is a dropbox link to the file
https://www.dropbox.com/s/t630m7oufn8ax2m/GTISL Game Sheet test.xlsm?dl=0

Any help to get this finished would be greatly appreciated.





Code:
Sub SendWith_SMTP_Gmail()
'Works On Windows (Not Mac). Mac Users Should Use Zapier Integration
'Created by Randy Austin www.ExcelForFreelancers.com
Dim EmailMsg, EmailConf As Object
Dim Subj, Mess, Json, URL, LastName, FirstName, Team, GameDate, GameTime, GameLocation, Field, Email, Attach As String
Dim ContactRow, LastRow, SentCounter As Long
Dim EmailFields As Variant
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") = "sctoronto2011boys@gmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "2011soccer"
        .Update
     End With
With Sheet1
  Subj = .Range("A53").Value 'Email Subject
  Mess = .Range("A54").Value 'Email Message
  Attach = .Range("F11").Value 'Attachment Link
  LastRow = .Range("M999").End(xlUp).Row 'Get Last Row Of Table
    
    For ContactRow = 2 To 52


        If .Range("I" & ContactRow).Value = "not scheduled this week" Then GoTo NextRow
        LastName = .Range("D" & ContactRow).Value
        FirstName = .Range("C" & ContactRow).Value
        Email = .Range("M" & ContactRow).Value
        Team = .Range("H" & ContactRow).Value
        GameDate = .Range("I" & ContactRow).Value
        GameTime = .Range("K" & ContactRow).Value
        GameLocation = .Range("J" & ContactRow).Value
        Field = .Range("L" & ContactRow).Value
        Subj = Replace(Replace(Subj, "#gamedate#", GameDate), "#LastName#", LastName)
        Mess = Replace(Replace(Mess, "#FirstName#", FirstName), "#LastName#", LastName)
       
        With EmailMsg
            Set .Configuration = EmailConf
            .To = Email
            .CC = ""
            .BCC = ""
            .From = """SC Toronto U9 Boys"" <sctoronto2011boys@gmail.com>"
            .Subject = Subj
             If Attach <> Empty Then .AddAttachment Attach
            .TextBody = Mess
            .Send
        End With
        SentCounter = SentCounter + 1
        .Range("P" & ContactRow).Value = Now 'Set Send Date & Time
NextRow:
    Next ContactRow
    
      'Cleanup
    Set EmailMsg = Nothing
    Set EmailConf = Nothing
    Set EmailFields = Nothing
End With
MsgBox SentCounter & " Emails have been sent"
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Change this

Code:
[COLOR=#333333].From = """SC Toronto U9 Boys"" [/COLOR]<sctoronto2011boys@gmail.com style="color: rgb(51, 51, 51); font-size: 12px;">"</sctoronto2011boys@gmail.com>

For this:

Code:
[COLOR=#333333].From = [/COLOR][COLOR=#333333]"sctoronto2011boys@gmail.com"[/COLOR]

Note: In your gmail account you must activate "Access to less secure applications"

https://www.google.com/settings/security/lesssecureapps
 
Upvote 0
Thanks Dante,
but that didn't work.

I have activated the less secure apps.
 
Upvote 0
I checked your file and the macro was pointing to another sheet.
I made some changes in your maro, I highlighted them in blue.

I tried the macro and it works for me.

Code:
Sub SendWith_SMTP_Gmail()
  'Works On Windows (Not Mac). Mac Users Should Use Zapier Integration
  'Created by Randy Austin www.ExcelForFreelancers.com
  Dim EmailMsg, EmailConf As Object
  Dim Subj, Mess, Json, URL, LastName, FirstName, Team, GameDate, GameTime, GameLocation, Field, Email, Attach As String
  Dim ContactRow, LastRow, SentCounter As Long, [COLOR=#0000ff]EmailUsr as String[/COLOR]
  Dim EmailFields As Variant
  '
[COLOR=#0000ff]  EmailUsr = "sctoronto2011boys@gmail.com"[/COLOR]
  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") = [COLOR=#0000ff]EmailUsr[/COLOR]
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "2011soccer"
    .Update
  End With
[COLOR=#0000ff]  With Sheets("[B]Roster[/B]")[/COLOR]
    Subj = .Range("[COLOR=#0000ff]B53[/COLOR]").Value 'Email Subject
    Mess = .Range("[COLOR=#0000ff]B55[/COLOR]").Value 'Email Message
    Attach = .Range("F11").Value 'Attachment Link
    LastRow = .Range("M999").End(xlUp).Row 'Get Last Row Of Table
    For ContactRow = 2 To 52
[COLOR=#0000ff]      If .Range("I" & ContactRow).Value [B]<>[/B] "not scheduled this week" Then[/COLOR]
        LastName = .Range("D" & ContactRow).Value
        FirstName = .Range("C" & ContactRow).Value
        Email = .Range("M" & ContactRow).Value
        Team = .Range("H" & ContactRow).Value
        GameDate = .Range("I" & ContactRow).Value
        GameTime = .Range("K" & ContactRow).Value
        GameLocation = .Range("J" & ContactRow).Value
        Field = .Range("L" & ContactRow).Value
        Subj = Replace(Replace(Subj, "#gamedate#", GameDate), "#LastName#", LastName)
        Mess = Replace(Replace(Mess, "#firstName#", FirstName), "#LastName#", LastName)
        '
        With EmailMsg
          Set .Configuration = EmailConf
          .To = Email
          .CC = ""
          .BCC = ""
          .From = [COLOR=#0000ff]EmailUsr[/COLOR]
          .Subject = Subj
          If Attach <> Empty Then .AddAttachment Attach
          .TextBody = Mess
          .Send
        End With
        SentCounter = SentCounter + 1
        .Range("P" & ContactRow).Value = Now 'Set Send Date & Time
[COLOR=#0000ff]      End If[/COLOR]
    Next ContactRow
    'Cleanup
    Set EmailMsg = Nothing
    Set EmailConf = Nothing
    Set EmailFields = Nothing
  End With
  MsgBox SentCounter & " Emails have been sent"
End Sub
 
Upvote 0
Thanks Dante,
It did send, but it is only pulling the info from the 1st row (row 2) and sending that to everyone. Each row has it's own info to pull into the merge and send to the email address on that row.
I also got an error
run-time
At least one recipient is require, but none were found.
 
Last edited:
Upvote 0
I put the code with more updates.


Add this line to append a file.
'Attach = "c:\folder\file name.pdf" 'folder name and file name

Changes in blue

Code:
Sub SendWith_SMTP_Gmail()
  'Works On Windows (Not Mac). Mac Users Should Use Zapier Integration
  'Created by Randy Austin www.ExcelForFreelancers.com
  Dim EmailMsg, EmailConf As Object, EmailFields As Variant, [COLOR=#0000ff]sh As Worksheet[/COLOR]
  Dim Subj, Mess, LastName, FirstName, Team, GameDate, GameTime, GameLocation, Field, Email, Attach As String
  Dim ContactRow, LastRow, SentCounter As Long, EmailUsr As String
  '
  EmailUsr = "sctoronto2011boys@gmail.com"
[COLOR=#ff0000]  'Attach = "c:\folder\file name.pdf"  'folder name and file name[/COLOR]
[COLOR=#0000ff]  Set sh = Sheets("Roster")[/COLOR]
  For ContactRow = 2 To [COLOR=#0000ff]50[/COLOR]
    If sh.Range("I" & ContactRow).Value <> "not scheduled this week" Then
      '
      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") = "2011soccer"
        .Update
      End With
      '
      Subj = sh.Range("B53").Value 'Email Subject
      Mess = sh.Range("B55").Value 'Email Message
      LastName = sh.Range("D" & ContactRow).Value
      FirstName = sh.Range("C" & ContactRow).Value
[COLOR=#ff0000]      Email = sh.Range("M" & ContactRow).Value   '[/COLOR]In column M you must have the email of each record.
      Team = sh.Range("H" & ContactRow).Value
      GameDate = sh.Range("I" & ContactRow).Value
      GameTime = sh.Range("K" & ContactRow).Value
      GameLocation = sh.Range("J" & ContactRow).Value
      Field = sh.Range("L" & ContactRow).Value
[COLOR=#0000ff]      Subj = Replace(Subj, "#gamedate#", GameDate)[/COLOR]
[COLOR=#0000ff]      Mess = Replace(Replace(Mess, "#firstname#", FirstName), "#lastname#", LastName)[/COLOR]
[COLOR=#0000ff]      Mess = Replace(Replace(Mess, "#location#", GameLocation), "#team#", Team)[/COLOR]
[COLOR=#0000ff]      Mess = Replace(Replace(Mess, "#gamedate#", GameDate), "#gametime#", GameTime)[/COLOR]
[COLOR=#0000ff]      Mess = Replace(Mess, "#field#", Field)[/COLOR]
      '
      With EmailMsg
        Set .Configuration = EmailConf
        .To = Email
        .CC = ""
        .BCC = ""
        .From = EmailUsr
        .Subject = Subj
        If Attach <> Empty Then .AddAttachment Attach
        .TextBody = Mess
[COLOR=#0000ff]        On Error Resume Next[/COLOR]
[COLOR=#0000ff]        .Send[/COLOR]
[COLOR=#0000ff]        On Error GoTo 0[/COLOR]
      End With
[COLOR=#0000ff]      If Err.Number = 0 Then[/COLOR]
[COLOR=#0000ff]        SentCounter = SentCounter + 1[/COLOR]
[COLOR=#0000ff]        sh.Range("P" & ContactRow).Value = Now 'Set Send Date & Time[/COLOR]
[COLOR=#0000ff]      Else[/COLOR]
[COLOR=#0000ff]        sh.Range("P" & ContactRow).Value = "Error : " & Err.Number & " " & Err.Description[/COLOR]
[COLOR=#0000ff]      End If[/COLOR]
    End If
    'Cleanup
    Set EmailMsg = Nothing
    Set EmailConf = Nothing
    Set EmailFields = Nothing
  Next ContactRow
  MsgBox SentCounter & " Emails have been sent"
End Sub
 
Upvote 0
Well that solved that problem. I can't begin to understand why, so I am just going to believe you are a ninja.

For some reason the first name is not changing. Everything else is working. Do you have any ideas why that is not working?
 
Upvote 0
For some reason the first name is not changing. Everything else is working. Do you have any ideas why that is not working?

What first name?

Ah, I think I know which name.

On the sheet you have this:
#firstname# is playing on the #team # on #gamedate # at #location # at #gametime # on #field #.
If you can't Make it, please let us know ASAP.
Please Arrive 30 mins before the game.
See you on the field.

In the macro is this
Code:
Mess = Replace(Replace(Mess, "#[COLOR=#ff0000]firstname[/COLOR]#", FirstName), "#lastname#", LastName)

You see, both must be lowercase. Fix the message on the sheet, put all #references # in lowercase.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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