VBA to send email from excel

srikanthg2310

New Member
Joined
Nov 9, 2015
Messages
17
Hi,

I am using the below macro to send email from my gmail to another email address (with attachment). However, I am getting an error when I run the code.
Can someone please fix the issue?



Sub CommandButton1_Click()
Dim Mail As New message
Dim x As Integer
x = 1

Do While Sheet1.Cells(x, 1) <> ""
Dim Config As Configuration: Set Config = Mail.Configuration

Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 465
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = "xxxxxxxxxx@gmail.com"
Config(cdoSendPassword) = "xxxxxxxx"
Config.Fields.Update



Mail.To = Sheet1.Cells(x, 1)
Mail.From = Config(cdoSendUserName)
Mail.Subject = Sheet1.Range("B2").Value
Mail.TextBody = Sheet1.Range("B1").Value
Mail.AddAttachment (Sheet1.Range("B4").Value)


x = x + 1


Mail.Send

Loop

MsgBox "Sent"

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
What does the error message say?
Which line of the macro does it stop at?

Check to have this reference active.
From the Tools menu, choose References to display the References dialog box.
In the References dialog box mark reference “Microsoft CDO for Windows 2000 Library”:
Image showing how to add the Microsoft CDO Library to the Office VBA Environment


And set up lesssecureapps in your gmail account:
 
Upvote 0
srikanthg2310, When you put code in the forum use code tag, to make it easier to read and copy

1650566391095.png


For example:
VBA Code:
Sub CommandButton1_Click()
  Dim Mail As New Message
  Dim x As Integer
  x = 1
  Do While Sheet1.Cells(x, 1) <> ""
    Dim Config As Configuration: Set Config = Mail.Configuration
    Config(cdoSendUsingMethod) = cdoSendUsingPort
    Config(cdoSMTPServer) = "smtp.gmail.com"
    Config(cdoSMTPServerPort) = 465
    Config(cdoSMTPAuthenticate) = cdoBasic
    Config(cdoSMTPUseSSL) = True
    Config(cdoSendUserName) = "xxxxxxxxxx@gmail.com"
    Config(cdoSendPassword) = "xxxxxxxx"
    Config.Fields.Update
    Mail.To = Sheet1.Cells(x, 1)
    Mail.From = Config(cdoSendUserName)
    Mail.Subject = Sheet1.Range("B2").Value
    Mail.TextBody = Sheet1.Range("B1").Value
    Mail.AddAttachment (Sheet1.Range("B4").Value)
    x = x + 1
    Mail.Send
  Loop
  MsgBox "Sent"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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