Gmail incl too many files as attachment

kjaer

New Member
Joined
Jul 24, 2013
Messages
8
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>
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,278
Try this...

Code:
[color=green]'Sending mail[/color]
[color=darkblue]With[/color] iMsg
    [color=darkblue]Set[/color] .Configuration = iConf
    .To = myCliMail
    .CC = ""
    .BCC = ""
    .From = """me"" "
    .Subject = myCliName
    .TextBody = strbody
   [B] .Attachments.DeleteAll[/B]
    .AddAttachment myCliFile
    .Send
[color=darkblue]End[/color] [color=darkblue]With[/color]
 

Watch MrExcel Video

Forum statistics

Threads
1,109,360
Messages
5,528,237
Members
409,809
Latest member
VICKRAM

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top