Hi everyone! (first post here)
I'm actually trying to send an email (using gmail) from Excel with VBA. Everything works just fine except two things.
First: There is an image that I want to appear on the left top of the mail. (business logo). You'll see in the code that I'm using HTML.body.
Second: I want my MAIL.AddAttachment to get the text inside of a worksheet cell (a cell that brings the info from a vlookup)
THE CODE:
Private Sub btnSendEmail_Click()
Dim MAIL As New Message
Dim config As Configuration
Set config = MAIL.Configuration
config(cdoSendUsingMethod) = cdoSendUsingPort
config(cdoSMTPServer) = "smtp.gmail.com"
config(cdoSMTPServerPort) = 25
config(cdoSMTPAuthenticate) = cdoBasic
config(cdoSMTPUseSSL) = True
config(cdoSendUserName) = "mail@mail.com"
config(cdoSendPassword) = "mailpassword"
config.Fields.Update
Call createJpg("MAIL", "A1:I49", "JPGname")
TempFilePath = Environ$("temp") & "\"
MAIL.AddAttachment TempFilePath & "JPGname.jpg", olByValue, 1
MAIL.To = Range("N14").Text
MAIL.CC = Range("N15").Text
MAIL.From = config(cdoSendUserName)
MAIL.Subject = "Hi " + Range("E17").Text + " // " + Range("N4").Text
MAIL.HTMLBody = "<span LANG=FR><p class=style2 p align=justify p style='width='850' height='1500'><span LANG=FR><font FACE=Calibri SIZE=3>" & _
"<IMG alt='' hspace=0 src='cid:MyPic.jpg' align=baseline border=0>" & _
"<p>Hello,<p/>" & _
"<p>blah blah blah,<p/>
MAIL.AddAttachment "Range(N17)"
On Error Resume Next
MAIL.Send
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "There was an error"
Exit Sub
End If
MsgBox "your email has been sent", vbInformation, "sent"
End Sub
___________________________________________________________________________________________________
Sub createJpg(Namesheet As String, nameRange As String, nameFile As String)
ThisWorkbook.Activate
Worksheets(Namesheet).Activate
Set Plage = ThisWorkbook.Worksheets(Namesheet).Range(nameRange)
Plage.CopyPicture
With ThisWorkbook.Worksheets(Namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.Width, Plage.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
End With
Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete
Set Plage = Nothing
End Sub
I'm actually trying to send an email (using gmail) from Excel with VBA. Everything works just fine except two things.
First: There is an image that I want to appear on the left top of the mail. (business logo). You'll see in the code that I'm using HTML.body.
Second: I want my MAIL.AddAttachment to get the text inside of a worksheet cell (a cell that brings the info from a vlookup)
THE CODE:
Private Sub btnSendEmail_Click()
Dim MAIL As New Message
Dim config As Configuration
Set config = MAIL.Configuration
config(cdoSendUsingMethod) = cdoSendUsingPort
config(cdoSMTPServer) = "smtp.gmail.com"
config(cdoSMTPServerPort) = 25
config(cdoSMTPAuthenticate) = cdoBasic
config(cdoSMTPUseSSL) = True
config(cdoSendUserName) = "mail@mail.com"
config(cdoSendPassword) = "mailpassword"
config.Fields.Update
Call createJpg("MAIL", "A1:I49", "JPGname")
TempFilePath = Environ$("temp") & "\"
MAIL.AddAttachment TempFilePath & "JPGname.jpg", olByValue, 1
MAIL.To = Range("N14").Text
MAIL.CC = Range("N15").Text
MAIL.From = config(cdoSendUserName)
MAIL.Subject = "Hi " + Range("E17").Text + " // " + Range("N4").Text
MAIL.HTMLBody = "<span LANG=FR><p class=style2 p align=justify p style='width='850' height='1500'><span LANG=FR><font FACE=Calibri SIZE=3>" & _
"<IMG alt='' hspace=0 src='cid:MyPic.jpg' align=baseline border=0>" & _
"<p>Hello,<p/>" & _
"<p>blah blah blah,<p/>
MAIL.AddAttachment "Range(N17)"
On Error Resume Next
MAIL.Send
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "There was an error"
Exit Sub
End If
MsgBox "your email has been sent", vbInformation, "sent"
End Sub
___________________________________________________________________________________________________
Sub createJpg(Namesheet As String, nameRange As String, nameFile As String)
ThisWorkbook.Activate
Worksheets(Namesheet).Activate
Set Plage = ThisWorkbook.Worksheets(Namesheet).Range(nameRange)
Plage.CopyPicture
With ThisWorkbook.Worksheets(Namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.Width, Plage.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
End With
Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete
Set Plage = Nothing
End Sub