Put cells from excel attachment in email body with VBA?

lost_in_the_sauce

Board Regular
Joined
Jan 18, 2021
Messages
128
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that uses the below VBA code to turn each tab into a separate excel file and attach it to an email addressed to the address in cell A2 of each tab. Is there a way to add cells A4:C5 to the body of the email so the recipients can seel the summary without opening the attachment?


VBA Code:
Sub Mail_Every_Worksheet()
  'Updateby ExtendOffice
  Dim xWs As Worksheet
  Dim xWb As Workbook
  Dim xFileExt As String, xTempFilePath As String, xFileName As String
  Dim xFileFormatNum As Long
  Dim xOlApp As Object, xMailObj As Object
  Dim sBody As String
 
  On Error Resume Next
  With Application
    .ScreenUpdating = False
    .EnableEvents = False
  End With
 
  xTempFilePath = Environ$("temp") & "\"
  If Val(Application.Version) < 12 Then
    xFileExt = ".xls": xFileFormatNum = -4143
  Else
    xFileExt = ".xlsm": xFileFormatNum = 52
  End If
 
  Set xOlApp = CreateObject("Outlook.Application")
  For Each xWs In ThisWorkbook.Worksheets
    If xWs.Range("A2").Value Like "?*@?*.?*" Then
      xWs.Copy
      Set xWb = ActiveWorkbook
      xFileName = xWs.Name
      Set xMailObj = xOlApp.CreateItem(0)
      xWb.Sheets.Item(1).Range("A2").Value = ""
      With xWb
        .SaveAs xTempFilePath & xFileName & xFileExt, FileFormat:=xFileFormatNum
        With xMailObj
          'specify the CC, BCC, Subject, Body below
          .To = xWs.Range("A2").Value
          .CC = ""
          .BCC = ""
          .Subject = "Update "
          sBody = "Monthly Payment Amounts"
          .Display
          .HtmlBody = sBody & .HtmlBody
          .Attachments.Add xWb.FullName
          .Display
        End With
        .Close SaveChanges:=False
      End With
      Set xMailObj = Nothing
      Kill xTempFilePath & xFileName & xFileExt
    End If
  Next
 
  Set xOlApp = Nothing
  With Application
    .ScreenUpdating = True
    .EnableEvents = True
  End With
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi there...

Untested... but maybe try by modifying the sBody variable in the code. You can concatenate the values of cells A4:C5 to the sBody variable, like this:

VBA Code:
sBody = "Monthly Payment Amounts" & "<br><br>" & _
"Summary:" & "<br>" & _
"Total Amount: " & xWs.Range("A4").Value & "<br>" & _
"Average Amount: " & xWs.Range("B4").Value & "<br>" & _
"Maximum Amount: " & xWs.Range("C4").Value

This will add a line break (<br>) between the header and the summary, as well as between each line of the summary. The & symbol is used to concatenate the strings. The _ symbol is used to continue the code on the next line for readability purposes. Make sure to adjust the headers and the cell references to match the actual content of cells A4:C5 in your workbook.
 
Upvote 0
Hi there...

Untested... but maybe try by modifying the sBody variable in the code. You can concatenate the values of cells A4:C5 to the sBody variable, like this:

VBA Code:
sBody = "Monthly Payment Amounts" & "<br><br>" & _
"Summary:" & "<br>" & _
"Total Amount: " & xWs.Range("A4").Value & "<br>" & _
"Average Amount: " & xWs.Range("B4").Value & "<br>" & _
"Maximum Amount: " & xWs.Range("C4").Value

This will add a line break (<br>) between the header and the summary, as well as between each line of the summary. The & symbol is used to concatenate the strings. The _ symbol is used to continue the code on the next line for readability purposes. Make sure to adjust the headers and the cell references to match the actual content of cells A4:C5 in your workbook.
It does work for pulling the values but I lose the formatting. Definitely something to keep in my back pocket for the future though, thank you
 
Upvote 0
Go to the link I posted…RangeToHtml is a function you need to add.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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