Reputation: 1
I am trying to send a PDF and an Excel spreadsheet page using CDO. I have it for most ISP but I cannot make it work for gmail.
I have an account and it works once in a while when I try it (go figure). I also, have a friend with a gmail account and I cannot get it to work ...ever with his account.
I have worked on this for 3 solid days and I give up. It will take someone much better that I to get it done. Below is the code I have tried without success .
Please help.
Sub SEND_PDF_SHEET_WITH_CDO()
On Error GoTo ErrHandler3:
Dim filepath As String
filepath = Environ$("temp") & "\" & ActiveWorkbook.Name & ".pdf" 'TODO:change filepath for the temp pdf file
Range("A5:P31").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
filepath, _
Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
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/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtp.gmail.com
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 ' I have tried 25, 465, 587 and more
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "MyEmail"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = MyPassword
.Update
End With
With iMsg
Set .Configuration = iConf
.From = "MyEmail" & "<[email protected]>" 'TODO:change email address here
.To = "MyEmail"
.Subject = "Hello"
.HTMLBody = Range("A350").Value
.AddAttachment (filepath)
.Send
End With
Set iMsg = Nothing
Set iConf = Nothing
Kill filepath
Exit Sub
ErrHandler3:
MsgBox "YOUR PDF E-MAIL DID NOT GO THROUGH. IT MAY BE YOU" _
& Chr$(13) _
& Chr$(13) _
& "HAVE NOT COMPLETED YOUR NON-OULOOK E-MAIL QUESTIONS" _
& Chr$(13) _
& Chr$(13) _
& "OR ENTERED THE INFORMATION INCORRECTLY." _
& Chr$(13) _
& Chr$(13) _
& "PLEASE TRY AGAIN AFTER RE-ENTERING YOUR INFORMATION."
Range("B8").Select
STOP_SUB = "YES"
Set iMsg = Nothing
Set iConf = Nothing
Kill filepath
Range("A1").Select
End Sub
Sub SEND_EXCEL_SHEET_WITH_CDO()
On Error GoTo ErrHandler2:
'Working in 97-2007
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim iMsg As Object
Dim iConf As Object
Dim Flds As Variant
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set Sourcewb = ActiveWorkbook
'Copy the ActiveSheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
'We exit the sub when your answer is NO in the security dialog that you only
'see when you copy a sheet from a xlsm file with macro's disabled.
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
Select Case Sourcewb.FileFormat
Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
Case 52:
If .HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If
Case 56: FileExtStr = ".xls": FileFormatNum = 56
Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
End Select
End If
End If
End With
'Save the new workbook/Mail it/Delete it
TempFilePath = Environ$("temp") & "\"
TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")
With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
.Close savechanges:=False
End With
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = Range("JA1").Value
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Range("JA2").Value
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = Range("JA3").Value
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Range("JA4").Value
.Update
End With
With iMsg
Set .Configuration = iConf
.To = "MyEmail"
.CC = ""
.BCC = ""
.From = "My Name" & "<[email protected]>"
.Subject = "HELLO"
.TextBody = "HELLO AGAIN" '<-- email body
.AddAttachment TempFilePath & TempFileName & FileExtStr
.Send
End With
'Delete the file you have send
Kill TempFilePath & TempFileName & FileExtStr
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Exit Sub
ErrHandler2:
MsgBox "YOUR EXCEL E-MAIL DID NOT GO THROUGH. IT MAY BE YOU" _
& Chr$(13) _
& Chr$(13) _
& "HAVE NOT COMPLETED YOUR NON-OULOOK E-MAIL QUESTIONS" _
& Chr$(13) _
& Chr$(13) _
& "OR ENTERED THE INFORMATION INCORRECTLY." _
& Chr$(13) _
& Chr$(13) _
& "PLEASE TRY AGAIN AFTER RE-ENTERING YOUR INFORMATION."
Range("B8").Select
STOP_SUB = "YES"
Kill TempFilePath & TempFileName
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Application.DisplayAlerts = False
ActiveWorkbook.Close
End Sub
Upvotes: 0
Views: 1294
Reputation: 31
This code works. PLUS it displays any errors which tell you why it didn't work.
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "[email protected]"
emailObj.To = "[email protected]"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
emailObj.AddAttachment "C:/Users/User/Desktop/err.fff"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "dc"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Ss"
emailConfig.Fields.Update
On Error Resume Next
emailObj.Send
If err.number = 0 then
Msgbox "Done"
Else
Msgbox err.number & " " & err.description
err.clear
End If
Also your account at www.gmail.com needs to be set to allow SMTP access.
The configuration info comes from Outlook Express (last in WinXP, renamed to Windows Mail in Vista, and dropped from Win7 and later). This shows default configuration on your computer.
Set emailObj = CreateObject("CDO.Message")
Set emailConfig = emailObj.Configuration
On Error Resume Next
For Each fld in emailConfig.Fields
msgbox fld.name & " = " & fld
Next
Also CDO for Windows 2000 is not always included in all editions/versions of windows. See http://support.microsoft.com/en-au/kb/171440
Upvotes: 2