Forum

Sending mail based ...
 
Notifications
Clear all

Sending mail based on excel data to GMAILS using VBA Script

9 Posts
3 Users
0 Reactions
93 Views
(@satyarankireddy)
Posts: 1
New Member
Topic starter
 

Hello Guru’s

I would require your help on below requirement. I am new in VBA script this is my first attempt.
Currently am sending mails one by one with manual change in below code. Now I am planning to change code in automation way.
I have excel with data as below.

SNO Name Mail ID Amount PDF attachement path
1 vardhan [email protected] 1000 D:vardhan.pdf
2 vihas [email protected] 1000 D:vihas.pdf
3 satya [email protected] 1000 D:Satya.pdf
4 varun [email protected] 1000 D:Varun.pdf

Based on above cell values need to send mail each one with pdf attachment in separately with one click.
Currently am using below code changing values manually one by one. It is consuming time.

Code :-
Sub send_email_via_Gmail()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") ="[email protected]"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"

myMail.Configuration.Fields.Update

With myMail
.Subject = "Test Email from Dr.xxx"
.From = "[email protected]"
.To = "[email protected]"
.CC = "[email protected]"
.BCC = ""
.TextBody = "Good morning!"
.AddAttachment "D:xxx.txt"
End With
On Error Resume Next
myMail.Send
'MsgBox ("Mail has been sent")
Set myMail = Nothing

End Sub

Can you please help or provide me code which is similar for my requirement. Highly appreciated.

 
Posted : 16/12/2018 7:07 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi,

I updated our code and inserted a loop. Make sure you change the column letters as needed, at this moment they are all pointing to column A to take data from, change the column in Tbl.Cells(i, "A").Value:

Sub send_email_via_Gmail()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
Dim Tbl as Worksheet, i as long
Set Tbl=ThisWorkbook.Worksheets("Sheet1")
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") ="[email protected]"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"

myMail.Configuration.Fields.Update

For i= 2 to tbl.Cells.Find("*", Tbl.Cells(1), , , xlByRows, xlPrevious).Row
With myMail
.Subject = "Test Email from Dr." & Tbl.Cells(i, "A").Value
.From = Cstr(Tbl.Cells(i, "A").Value)
.To = Cstr(Tbl.Cells(i, "A").Value)
.CC = Cstr(Tbl.Cells(i, "A").Value)
.BCC = ""
.TextBody = "Good morning!"
.AddAttachment Cstr(Tbl.Cells(i, "A").Value)
End With
'On Error Resume Next
myMail.Send
Next
'MsgBox ("Mail has been sent")
Set myMail = Nothing

End Sub

 
Posted : 20/12/2018 1:17 am
(@manthu)
Posts: 4
New Member
 

Hi It worked but the attachment macro seems to add all the attachments for each sender. Could you please see if there is an alternative coding you can suggest?

 
Posted : 20/06/2021 5:01 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi,

You can reorganize the code:

Sub SendAll()
Dim Tbl as Worksheet, i as long
Set Tbl=ThisWorkbook.Worksheets("Sheet1")
For i= 2 to tbl.Cells.Find("*", Tbl.Cells(1), , , xlByRows, xlPrevious).Row
send_email_via_Gmail i, Tbl
Next
End Sub

Sub send_email_via_Gmail(ByVal i as long, Byval Tbl as ListObject)
Dim myMail As CDO.Message
Set myMail = New CDO.Message

myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") ="[email protected]"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"

myMail.Configuration.Fields.Update

With myMail
.Subject = "Test Email from Dr." & Tbl.Cells(i, "A").Value
.From = Cstr(Tbl.Cells(i, "A").Value)
.To = Cstr(Tbl.Cells(i, "A").Value)
.CC = Cstr(Tbl.Cells(i, "A").Value)
.BCC = ""
.TextBody = "Good morning!"
.AddAttachment Cstr(Tbl.Cells(i, "A").Value)
'On Error Resume Next
.Send
End With

'MsgBox ("Mail has been sent")
Set myMail = Nothing

End Sub

 
Posted : 23/06/2021 1:47 am
(@manthu)
Posts: 4
New Member
 

Catalin Bombea,

You're A Genius

 
Posted : 23/06/2021 9:43 am
(@manthu)
Posts: 4
New Member
 

Catalin Bombea,

Could you please help me out for the coding for gmail to draft the email but not send it. 
For outlook its .Display but for gmail, been trying many ways, no luck!

would be of great help! Thx

 
Posted : 27/06/2021 4:18 pm
(@catalinb)
Posts: 1937
Member Admin
 

Not possible.

That's CDO (not gmail) and it's a library, not an email client like outlook.

 
Posted : 27/06/2021 10:54 pm
(@manthu)
Posts: 4
New Member
 

Oh ok. Actually i want to add my default signature before sending emails. Is there any way i can do that?

thanks for all your advice!

 
Posted : 29/06/2021 2:30 am
(@catalinb)
Posts: 1937
Member Admin
 

CDO is not outlook, thought I just mentioned that.

Are you sure you have a CDO signature? To help you answer this question, there is no signature in CDO, you might have one in an EMAIL CLIENT (like outlook, thunderbird).

Where is your default signature?

Just format the .HTMLBody in cdo (instead of .TextBody) to build your signature directly in code.

 
Posted : 29/06/2021 3:05 am
Share: