Why does this VBA Macro that copies and paste range into email in the wrong order

0 votes

Writing a macro to copy and paste a range into an Outlook email is what I'm attempting to do. Below the pasted email's pasted range, I want to insert some text and formatting.

Sub sendEmailwithPic()

Dim OutApp As Object
Dim OutMail As Object
Dim table As Range
Dim pic As Picture
Dim ws As Worksheet
Dim wordDoc


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

'grab table, convert to image, and cut
Set ws = ThisWorkbook.Sheets("Sheet1")
Set table = ws.Range("A1:E11")
ws.Activate
table.Copy
Set pic = ws.Pictures.Paste
pic.Cut

'create email message
On Error Resume Next
    With OutMail
        .to = "someone@gmail.com"
        .Subject = "Country Population Data " & Format(Date, "mm-dd-yy")
        .Display
        
        Set wordDoc = OutMail.GetInspector.WordEditor
        wordDoc.Range.PasteandFormat wdChartPicture
        .HTMLBody = "<body style=font-size:11pt;font-family:Calibri>" & _
                "<p><font style=font-size:11pt;font-family:Calibri><b>Lorem ipsum dolor sit amet, consectetur adipiscing elit," & _
                "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Vitae ultricies leo integer malesuada nunc.</b></p>" & _
                "<p><font style=font-size:9pt;font-family:Calibri>Lorem ipsum dolor sit amet, consectetur adipiscing elit," & _
                "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>" & _
                "<p><font style=font-size:9pt;font-family:Arial;color:#595959><i>Lorem ipsum dolor sit amet, consectetur adipiscing elit," & _
                "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Amet consectetur adipiscing elit ut. Mattis aliquam faucibus purus in massa tempor." & _
                "Hendrerit dolor magna eget est.</i></font></p>" & .HTMLBody
    End With
    On Error GoTo 0
    
Set OutApp = Nothing
Set OutMail = Nothing

End Sub

When the macro compiles the email that is displayed looks like this:enter image description here

However, my desired outcome is this: enter image description here

Can someone help me figure out what I'm doing wrong?

Mar 21, 2023 in Others by narikkadan
• 63,420 points
460 views

1 answer to this question.

0 votes
When a message body is created by utilising the Word object model and the HTMLBody property. I hope this helps you.
answered Mar 21, 2023 by Kithuzzz
• 38,010 points

Related Questions In Others

0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

Export All appointments and meetings (including recurring meetings) Excel VBA

However, when I use the code above ...READ MORE

answered Feb 13, 2023 in Others by narikkadan
• 63,420 points
1,162 views
0 votes
1 answer

Excel VBA creating a new Outlook appointment results in a cancelled appointment

Because an inappropriate sender will be used, ...READ MORE

answered Feb 14, 2023 in Others by Kithuzzz
• 38,010 points
595 views
0 votes
1 answer

Embed picture in outlook mail body excel vba

The image needs to be added and ...READ MORE

answered Dec 16, 2022 in Others by narikkadan
• 63,420 points
4,508 views
0 votes
1 answer

VBA How to extract the date and time of arrival of a answered email

Use MailItem.ReceivedTime property. I hope this helps you ...READ MORE

answered Jan 9, 2023 in Others by narikkadan
• 63,420 points
2,131 views
0 votes
1 answer
0 votes
1 answer

VBA to protect and unprotect in given range in sheet

It needs a minor tweak to become ...READ MORE

answered Nov 24, 2022 in Others by Kithuzzz
• 38,010 points
1,895 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP