CDO mail

Fire_Chief

Well-known Member
Joined
Jun 21, 2003
Messages
693
Office Version
  1. 365
Platform
  1. Windows
I need the code to send CDO mail to a gmail account. I have tried many times to get

Ron de Bruins code to work but just can't get it.

VBA Code:
Sub SEND_PDF_SHEET_WITH_CDO()

Dim filepath As String

filepath = Environ$("temp") & "\" & ActiveWorkbook.Name & ".pdf" 'TODO:change filepath for the temp pdf file

    Range("A5:P39").Select                          ' In excel worksheet

    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        filepath, _
        Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
'Setting up CDOSYS configuration to send out the email
Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
    .Item("http://schemas.Microsoft.Com/cdo/configuration/smtpusetls") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "someone@somewhere.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = my password
    .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") = 587
    .Update
    End With

    With iMsg
         Set .Configuration = iConf
        .From = "someone@somewhere.com"
       .To = Range("JA3").Value
         .Subject = Range("A346").Value
        .HTMLBody = Range("A350").Value
        .AddAttachment (filepath)
        .Send                                                       ' THIS IS WHERE I GET AN ERROR   = Run_time error '-2147220973 (80040213)':
    End With

    Set iMsg = Nothing
    Set iConf = Nothing
     Kill filepath
    Set iMsg = Nothing
    Set iConf = Nothing
     Kill filepath

End Sub


This is driving me crazy.
 
Last edited by a moderator:
This should send range A1 thur A5...
If you have gmail



Sub SEND_PDF_SHEET_WITH_CDO()

Dim filepath As String

filepath = Environ$("temp") & "\" & ActiveWorkbook.Name & ".pdf" 'TODO:change filepath for the temp pdf file
On Error GoTo ErrHandler3:

Range("A1:A5").Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
filepath, _
Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
'Setting up CDOSYS configuration to send out the email
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
.Item("http://schemas.Microsoft.Com/cdo/configuration/smtpusetls") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = True
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Your full gmail address here"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Your Password"
.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
.Update
End With

With iMsg
Set .Configuration = iConf
.From = "Your full gmail address here"

.To = "Your full email here"
JUMP_TO_SUBJECT:
.Subject = "TEST"
.HTMLBody = "HELLO"
.AddAttachment (filepath) 'This is Range "A1:A5" of the worksheet that is being sent
.Send
End With

Set iMsg = Nothing
Set iConf = Nothing
Kill filepath

MsgBox "YOUR E-MAIL WAS SENT. "
Exit Sub

ErrHandler3:

MsgBox "YOUR E-MAIL DID NOT GO THROUGH. "

Set iMsg = Nothing
Set iConf = Nothing
Kill filepath
Range("A1").Select
End Sub
Thank you but I do not want to send PDF I only want text in A5 to send as email body
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,216,471
Messages
6,130,822
Members
449,595
Latest member
jhester2010

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