• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Send Email with vba

Monty

Well-Known Member
Hello Excel Vba Experts.

This is regarding sending email to outlook by using excel vba wanted to send personalised email with name with image display..Image will not be same everytime..
i replace new image..vba should pick the image and send mail.

Attatched excel file for a quick understanding and required output.
 

Attachments

  • Email Sample.xlsb
    82 KB · Views: 8
Hello Excel Vba Experts.

This is regarding sending email to outlook by using excel vba wanted to send personalised email with name with image display..Image will not be same everytime..
i replace new image..vba should pick the image and send mail.

Attatched excel file for a quick understanding and required output.
Hi @Monty

Please see attached

Note that this code exports the image to "D:", uses it for the emails and then permanently deletes it... you may have to change this path. If you need help with this just let me know.
Also, I commented the .send part so you can see the email before sending... you can uncomment that line and comment .Display and it should send the emails without showing them.

Code:
Option Explicit

Sub ExportMyPicture()

    Dim MyChart As String, MyPicture As String
    Dim PicWidth As Long, PicHeight As Long

    Application.ScreenUpdating = False
    On Error GoTo Finish

    ActiveSheet.Shapes.Range(Array("Picture 1")).Select
    MyPicture = Selection.Name
    With Selection
          PicHeight = .ShapeRange.Height
          PicWidth = .ShapeRange.Width
    End With

    Charts.Add
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
    Selection.Border.LineStyle = 0
    MyChart = Selection.Name & " " & Split(ActiveChart.Name, " ")(2)

    With ActiveSheet
          With .Shapes(MyChart)
                .Width = PicWidth
                .Height = PicHeight
          End With

          .Shapes(MyPicture).Copy

          With ActiveChart
                .ChartArea.Select
                .Paste
          End With

          .ChartObjects(1).Chart.Export Filename:="D:MyPic.jpg", FilterName:="jpg"
          .Shapes(MyChart).Cut
    End With
 
    Application.ScreenUpdating = True
    Exit Sub

Finish:
    MsgBox "You must select a picture"
End Sub

Sub Send_Email()

    ExportMyPicture

    Dim c As Range
    Dim OutLookApp As Object
    Dim OutLookMailItem As Object
    Dim olbyvalue As Integer
 
    For Each c In Range("A2:A" & Columns("A").Cells(Rows.Count).End(xlUp).Row).Cells
 
        Set OutLookApp = CreateObject("Outlook.application")
        Set OutLookMailItem = OutLookApp.CreateItem(0)

        With OutLookMailItem
            .To = c.Value
            .Subject = "Insert Subject here"
            .HTMLBody = "Hello " & c.Offset(0, 1).Value & "<br>" & "<br>" & "This is a test mail" & "<br>"
            .Attachments.Add "D:MyPic.jpg", _
                olbyvalue, 0
            .HTMLBody = .HTMLBody & "<br>" & "<img src='cid:MyPic.jpg'"
            .Display
'            .Send
        End With

    Next c
 
    On Error Resume Next
    Kill "D:MyPic.jpg"

End Sub
 

Attachments

  • Email Sample.xlsb
    92.3 KB · Views: 19
Back
Top