Macro to extract text to Body in Email and Table at end of Body

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,570
Office Version
  1. 2021
Platform
  1. Windows
I have code below to generate an email and the body is extracted using the range name "Bodytext" and then to extract data in E1 to last row in Col G and paste this as a table after the last text in the body of the email.


I need help with the following

1) The range name body text to appear in the body of the email with the same page breaks as the named range "bodyText"
2) The Ageing in Col F to be shown with zero decimals when pasted in the body of the email

Code:
 Sub Email_Report()
    
    ThisWorkbook.Activate ' Start in THIS workbook
    Dim zSubject As String
    Dim OutApp As Object, OutMail As Object
    Dim zText As String
    Dim lastRow As Long, i As Long
    Dim regex As Object, matches As Object, match As Object

    ' Read subject from named cell
    zSubject = Sheets("Email").Range("subjectText").Value
    
    ' Read body text from cell B2 ("BodyText") on sheet "Email"
    zText = Sheets("Email").Range("BodyText").Value
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    
    With ThisWorkbook.Sheets("Email")
        lastRow = .Cells(.Rows.Count, "E").End(xlUp).Row ' Find the last row in column E
        
        ' Initialize the email body with the content from "BodyText"
        zText = "<html><body>" & Replace(zText, vbCrLf, "<br>") & "</body></html>"
        
        ' Add the table header to the email body
        zText = zText & "<br><br><html><table border='1' cellpadding='5'><tr><th>Reference</th><th>Ageing</th><th>Amount</th></tr>"
        
        ' Add the data from F1 to last row in column G to the email body as table rows
        For i = 2 To lastRow ' Start from row 2 to skip headers
            zText = zText & "<tr><td>" & .Cells(i, "E").Value & "</td><td>" & .Cells(i, "F").Value & "</td><td>" & .Cells(i, "G").Value & "</td></tr>"
        Next i
        
        ' Close the table tag and complete the email body
        zText = zText & "</table></html>"
        
        ' Use regular expression to add line breaks after periods that are not part of a number
        Set regex = CreateObject("VBScript.RegExp")
        regex.Global = True
        regex.IgnoreCase = True
        regex.Pattern = "([^0-9])\.\s"
        
        If regex.Test(zText) Then
            Set matches = regex.Execute(zText)
            For Each match In matches
                ' Check if the match is followed by a number, indicating a decimal number
                If IsNumeric(Mid(zText, match.FirstIndex + Len(match.Value) + 1, 1)) Then
                    ' Do not add line breaks
                Else
                    ' Add two line breaks after the period
                    zText = Replace(zText, match.Value, match.Value & "<br><br>")
                End If
            Next match
        End If
    End With

    With OutMail
        ActiveWorkbook.Save
        .To = Sheets("Email").Range("N1").Value
        .CC = Join(Application.Transpose(Sheets("Email").Range("N2:N5").Value), ";")
        .BCC = ""
        .Subject = zSubject
        .HTMLBody = zText
        .Attachments.Add ActiveWorkbook.FullName
        .Display
    End With

    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub




I have shared the link below


I have also posted on



Your assistance is most appreciated
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Through trial and error, I finally came up with the following solution to suit my needs

Code:
 Sub Email_Report()
    
    ThisWorkbook.Activate ' Start in THIS workbook
    Dim zSubject As String
    Dim OutApp As Object, OutMail As Object
    Dim zText As String
    Dim lastRow As Long, i As Long
    Dim regex As Object, matches As Object, match As Object

    ' Read subject from named cell
    zSubject = Sheets("Email").Range("subjectText").Value
    
    ' Read body text from cell B2 ("BodyText") on sheet "Email"
    zText = Sheets("Email").Range("BodyText").Value
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    
    With ThisWorkbook.Sheets("Email")
        lastRow = .Cells(.Rows.Count, "E").End(xlUp).Row ' Find the last row in column E
        
        ' Initialize the email body with the content from "BodyText"
        zText = "<html><body>" & Replace(zText, vbCrLf, "<br>") & "</body></html>"
        
        ' Add the table header to the email body
        zText = zText & "<br><br><html><table border='1' cellpadding='5'><tr><th>Reference</th><th>Ageing</th><th>Amount</th></tr>"
        
        ' Add the data from F1 to last row in column G to the email body as table rows
        For i = 2 To lastRow ' Start from row 2 to skip headers
            zText = zText & "<tr><td>" & .Cells(i, "E").Value & "</td><td>" & .Cells(i, "F").Value & "</td><td>" & .Cells(i, "G").Value & "</td></tr>"
        Next i
        
        ' Close the table tag and complete the email body
        zText = zText & "</table></html>"
        
        ' Use regular expression to add line breaks after periods that are not part of a number
        Set regex = CreateObject("VBScript.RegExp")
        regex.Global = True
        regex.IgnoreCase = True
        regex.Pattern = "([^0-9])\.\s"
        
        If regex.Test(zText) Then
            Set matches = regex.Execute(zText)
            For Each match In matches
                ' Check if the match is followed by a number, indicating a decimal number
                If IsNumeric(Mid(zText, match.FirstIndex + Len(match.Value) + 1, 1)) Then
                    ' Do not add line breaks
                Else
                    ' Add two line breaks after the period
                    zText = Replace(zText, match.Value, match.Value & "<br><br>")
                End If
            Next match
        End If
    End With

    With OutMail
        ActiveWorkbook.Save
        .To = Sheets("Email").Range("N1").Value
        .CC = Join(Application.Transpose(Sheets("Email").Range("N2:N5").Value), ";")
        .BCC = ""
        .Subject = zSubject
        .HTMLBody = zText
        .Attachments.Add ActiveWorkbook.FullName
        .Display
    End With

    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,949
Messages
6,127,880
Members
449,411
Latest member
AppellatePerson

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