Emailing contents and placing in the body of the mail

wigarth

Board Regular
Joined
Apr 16, 2016
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Hi!

Trying to make a vba that sends an array into the body of an email as an image.
The code i have provided here works great, but the ".body" text always comes below the picture somehow.

Is there a way to make it come on top?

VBA Code:
Sub emailcharts()

    Dim ol As Outlook.Application
    Dim MyEml As Outlook.MailItem
    Dim doc As Word.Document
    Dim rng As Range
    
    Set rng = Sheets("Epost").Range("f4:o59")
    Set ol = New Outlook.Application
    Set MyEml = ol.CreateItem(olMailItem)

rng.Copy

MyEml.Body = vbCr & ThisWorkbook.Sheets("Epost").Range("H2").Value & " " & ThisWorkbook.Sheets("Epost").Range("I2").Value & vbCr & vbCr & _
"Dette er tallene så langt" & vbCr & vbCr

MyEml.Display
    Set doc = MyEml.GetInspector.WordEditor
    
    MyEml.To = ThisWorkbook.Sheets("Epost").Range("c6").Value
    MyEml.Subject = ThisWorkbook.Sheets("Epost").Range("H2").Value & " " & ThisWorkbook.Sheets("Epost").Range("I2").Value

doc.Range(0, 0).PasteAndFormat Type:=wdChartPicture

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try the following...

VBA Code:
Sub emailcharts()

    Dim ol As Outlook.Application
    Dim MyEml As Outlook.MailItem
    Dim doc As Word.Document
    Dim rng As Range
    
    Set rng = Sheets("Epost").Range("f4:o59")
    Set ol = New Outlook.Application
    Set MyEml = ol.CreateItem(olMailItem)

    rng.Copy

    With MyEml
        .Display
        .To = ThisWorkbook.Sheets("Epost").Range("c6").Value
        .Subject = ThisWorkbook.Sheets("Epost").Range("H2").Value & " " & ThisWorkbook.Sheets("Epost").Range("I2").Value
        Set doc = .GetInspector.WordEditor
        doc.Range.PasteAndFormat Type:=wdChartPicture
        .HTMLBody = "<p>" & ThisWorkbook.Sheets("Epost").Range("H2").Value & " " & ThisWorkbook.Sheets("Epost").Range("I2").Value & "</p>" & _
                "<p>Dette er tallene så langt</p>" & .HTMLBody
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Try the following...

VBA Code:
Sub emailcharts()

    Dim ol As Outlook.Application
    Dim MyEml As Outlook.MailItem
    Dim doc As Word.Document
    Dim rng As Range
   
    Set rng = Sheets("Epost").Range("f4:o59")
    Set ol = New Outlook.Application
    Set MyEml = ol.CreateItem(olMailItem)

    rng.Copy

    With MyEml
        .Display
        .To = ThisWorkbook.Sheets("Epost").Range("c6").Value
        .Subject = ThisWorkbook.Sheets("Epost").Range("H2").Value & " " & ThisWorkbook.Sheets("Epost").Range("I2").Value
        Set doc = .GetInspector.WordEditor
        doc.Range.PasteAndFormat Type:=wdChartPicture
        .HTMLBody = "<p>" & ThisWorkbook.Sheets("Epost").Range("H2").Value & " " & ThisWorkbook.Sheets("Epost").Range("I2").Value & "</p>" & _
                "<p>Dette er tallene så langt</p>" & .HTMLBody
    End With
   
End Sub

Hope this helps!
Worked perfectly. Many thanks to you! Still dont understand how this specifies it to be on top, but my macro now works :) (Is it the " & .htmlbody!" at the end?)
 
Upvote 0
Yes, that's correct. Basically, the contents of .HTMLBody gets replaced with text AND whatever is already in .HTMLBody, which in this case is the picture.

Cheers!
 
Upvote 0
You're very welcome, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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