GMAIL Excel VBA (add.attachment & Images)

VBAzr

New Member
Joined
Oct 28, 2014
Messages
10
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
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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