+ Reply to Thread
Results 1 to 3 of 3

VBA Code to extract a Tables Data into a Image into Outlook Body

  1. #1
    Registered User
    Join Date
    05-19-2023
    Location
    London, England
    MS-Off Ver
    MS Office 2016
    Posts
    2

    VBA Code to extract a Tables Data into a Image into Outlook Body

    Hi Everyone,

    I am new to VBA coding, and am hitting brick wall with Youtube. It was suggested this forum/site was fantastic for Excel/VBA support

    I have created a basic data entry user form in Excel; but I wanted a command button called "Draft Email" to create a draft email in Outlook with the subject, main body text and an image of the table. However, the problem I am having is I can't seem to have the code select only the top row containing the headers and the last row; as this would be the new data entered each time.

    I had it working, but it would put in the whole table, and then when I changed it to get the last row if wouldn't provide the top row 'Headers' anymore.

    My coding is below, I just cant see what I am doing wrong any help anyone could provide would be amazing.

    Code for whole table in Outlook

    Private Sub Draft_Email_Click()

    Sheets("Data").Unprotect password:="HealthandSafety01"

    Dim OutApp As Object
    Dim OutAMail As Object
    Dim table As Range
    Dim Lastrow As Long
    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("Data")
    Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set table = ws.Range("A1:I" & Lastrow)
    ws.Activate
    table.Copy
    Set pic = ws.Pictures.Paste
    pic.Cut

    ' create email message

    On Error Resume Next

    With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "Fire Marshall Fire Checks " & Format(Date, "DD-MM-YY")
    .Display

    Set wordDoc = OutMail.GetInspector.WordEditor
    With wordDoc.Range
    .Range("").Font.Size = 20
    .PasteandFormat wdChartPicture
    With .InlineShapes(.InlineShapes.Count)
    .LockAspectRatio = msoFalse
    .Width = 1800
    .Height = 200
    .InsertParagraghAfter
    .InsertParagraghAfter
    .InsertParagraghAfter
    .InsertAfter ""
    .InsertParagraghAfter
    .InsertParagraghAfter
    .InsertParagraghAfter

    End With

    End With

    .HTMLBody = "<BODY style = font-siz:12pt; font-family;Arial>" & _
    "Dear All<p>Please find attached the fire safety checks completed today." & .HTMLBody

    End With

    On Error GoTo 0

    Set OutApp = Nothing
    Set OutMail = Nothing

    Sheets("Data").Protect password:="HealthandSafety01"

    End Sub

    Code for just the last row on new entered data

    Private Sub Draft_Email_Click()

    Sheets("Data").Unprotect password:="HealthandSafety01"

    Dim OutApp As Object
    Dim OutAMail As Object
    Dim table As Range
    Dim Lastrow As Long
    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("Data")
    Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set table = ws.Range("A" & Lastrow & ":I" & Lastrow)
    ws.Activate
    table.Copy
    Set pic = ws.Pictures.Paste
    pic.Cut

    ' create email message

    On Error Resume Next

    With OutMail
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = "Fire Marshall Fire Checks " & Format(Date, "DD-MM-YY")
    .Display

    Set wordDoc = OutMail.GetInspector.WordEditor
    With wordDoc.Range
    .Range("").Font.Size = 20
    .PasteandFormat wdChartPicture
    With .InlineShapes(.InlineShapes.Count)
    .LockAspectRatio = msoFalse
    .Width = 1800
    .Height = 200
    .InsertParagraghAfter
    .InsertParagraghAfter
    .InsertParagraghAfter
    .InsertAfter ""
    .InsertParagraghAfter
    .InsertParagraghAfter
    .InsertParagraghAfter

    End With

    End With

    .HTMLBody = "<BODY style = font-siz:12pt; font-family;Arial>" & _
    "Dear All<p>Please find attached the fire safety checks completed today." & .HTMLBody

    End With

    On Error GoTo 0

    Set OutApp = Nothing
    Set OutMail = Nothing

    Sheets("Data").Protect password:="HealthandSafety01"

    End Sub

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,450

    Re: VBA Code to extract a Tables Data into a Image into Outlook Body

    You could either do it as 2 images, one for the header and one for the last row.

    Or hide the rows in between, copypicture and then unhide rows.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-19-2023
    Location
    London, England
    MS-Off Ver
    MS Office 2016
    Posts
    2

    Re: VBA Code to extract a Tables Data into a Image into Outlook Body

    Hi Andy,

    Your code worked perfectly. Thank you so much.

    Regards,
    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA extract Data from specific location in body of Outlook To Excel
    By korekurd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2020, 10:04 AM
  2. Copy an image and range of text from Excel to Outlook mail body
    By ARance077 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-14-2020, 03:46 AM
  3. [SOLVED] Extract Values from sheet to put on body of outlook
    By flupsie in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-01-2018, 01:14 AM
  4. Replies: 0
    Last Post: 09-07-2015, 04:05 PM
  5. Need help copying a picture/image from excel to the body of an outlook email
    By kadames27 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-15-2014, 10:17 AM
  6. sending mail in outlook with image in Top center of body .
    By yogananda.muthaiah in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-25-2012, 05:06 PM
  7. Macro to extract body data from Outlook messages in to Excel
    By gangup in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-23-2012, 12:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1