JPG email by VBA - Blank email

ashani

Active Member
Joined
Mar 14, 2020
Messages
345
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm using the below code to convert the range to JPG and email. However, if I put .send than the blank email goes out but when I put .display than email is fine. Please can you help me that where I'm going wrong as I want email to go out automatically rather than manually clicking send button.

Thank you

VBA Code:
Sub jpgpdfrtw()

Dim OutApp As Object
Dim OutMail As Object
Dim table As Range
Dim pic As Picture
Dim ws As Worksheet
Dim wordDoc
Dim a As String, b As String, c1 As String, c2 As String, c3 As String, d As String
Dim IsCreated As Boolean
Dim i As Long
Dim j As Long
Dim PdfFile As String, Title As String
Dim fName As String
Dim OutlApp As Object
Dim oItem As Object
Const olMailItem As Long = 0

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

Set ws = ThisWorkbook.Sheets("ABC")
Set table = ws.Range("A1:J36")
ws.Activate
table.CopyPicture
Set pic = ws.Pictures.Paste
pic.Cut

On Error Resume Next
With OutMail
.To = Range("N3").Value
.CC = Range("N5").Value & "; " & Range("O10").Value
.Subject = "ABC" & " " & ActiveSheet.Range("B7").Value
.send
    
Set wordDoc = OutMail.GetInspector.WordEditor
            With wordDoc.Range
                .PasteandFormat wdChartPicture
                .insertParagraphAfter
                .insertParagraphAfter
                .InsertAfter "Many thanks,"
                .insertParagraphAfter
                .InsertAfter
            End With

.htmlbody = "<BODY style = font-size:11pt; font-family:Calibri >" & _
            "Hi, <p> ABCDEFGH. <p>" & .htmlbody

    
On Error Resume Next
    Application.Visible = True
    If Err Then
      MsgBox "E-mail was not sent", vbExclamation
    Else
      MsgBox "Email sent", vbInformation
    End If
    On Error GoTo 0
        
    
Set OutApp = Nothing
Set OutMail = Nothing

Application.CutCopyMode = False

End With


End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Well, looking at your code, it's pretty clear that's because you're literally sending it before the you let VBA paste the image into the email:

*********************************************************************************
...
.send
<- You're sending it here

Set wordDoc = OutMail.GetInspector.WordEditor
With wordDoc.Range
.PasteandFormat wdChartPicture <- You're pasting the image and filling in the text here
.insertParagraphAfter
.insertParagraphAfter
...
*********************************************************************************
Try moving the .Send to just before the On Error Resume Next line so it looks like:
VBA Code:
.Send
On Error Resume Next
 
Upvote 0
But you provided a detailed explanation, so that's good . . . (y):)
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,665
Members
449,091
Latest member
peppernaut

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