Hi guys
I've created an excel macro that basically sends an email with an attachment included. I'm using gmail as mailsystem.
Basically everything works fine, except for that fact that my attachment from first email generated will be included in my second email generated, both attachments from second email will be included in my third email etc. In other words every email contains every attachment included in previous emails.
What am I doing wrong
?
Thanks,
Kjaer
This is (most of) my code:
Sub gmail ()
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Dim myCliName As String
Dim myCliMail As String
Dim myCliFile As String
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/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemail@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
strbody = "Hello" & vbNewLine & vbNewLine & _
"bla bla bla" & vbNewLine & _
"best regards"
' Identify recipient data
myWS.Select
Dim myCell As Range, myRng As Range
Set myRng = Range("A2", Range("A2").End(xlDown))
myMailCount = 0
For Each myCell In myRng
myCliName = myCell.Value
myCliMail = myCell.Offset(0, 1).Value
myCliFile = myCell.Offset(0, 3).Value
'Sending mail
With iMsg
Set .Configuration = iConf
.To = myCliMail
.CC = ""
.BCC = ""
.From = """me"" <myemail@gmail.com>"
.Subject = myCliName
.TextBody = strbody
.AddAttachment myCliFile
.Send
End With
myMailCount = myMailCount + 1
myCliName = Empty
myCliMail = Empty
myCliFile = Empty
Next myCell
MsgBox myMailCount & " emails have been sent", vbInformation, "Emails sent"
End Sub</myemail@gmail.com>
I've created an excel macro that basically sends an email with an attachment included. I'm using gmail as mailsystem.
Basically everything works fine, except for that fact that my attachment from first email generated will be included in my second email generated, both attachments from second email will be included in my third email etc. In other words every email contains every attachment included in previous emails.
What am I doing wrong

Thanks,
Kjaer
This is (most of) my code:
Sub gmail ()
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Dim myCliName As String
Dim myCliMail As String
Dim myCliFile As String
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/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemail@gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
strbody = "Hello" & vbNewLine & vbNewLine & _
"bla bla bla" & vbNewLine & _
"best regards"
' Identify recipient data
myWS.Select
Dim myCell As Range, myRng As Range
Set myRng = Range("A2", Range("A2").End(xlDown))
myMailCount = 0
For Each myCell In myRng
myCliName = myCell.Value
myCliMail = myCell.Offset(0, 1).Value
myCliFile = myCell.Offset(0, 3).Value
'Sending mail
With iMsg
Set .Configuration = iConf
.To = myCliMail
.CC = ""
.BCC = ""
.From = """me"" <myemail@gmail.com>"
.Subject = myCliName
.TextBody = strbody
.AddAttachment myCliFile
.Send
End With
myMailCount = myMailCount + 1
myCliName = Empty
myCliMail = Empty
myCliFile = Empty
Next myCell
MsgBox myMailCount & " emails have been sent", vbInformation, "Emails sent"
End Sub</myemail@gmail.com>