Copy Range from Multiple Sheets into the Body of Multiple Emails

philk228

New Member
Joined
Jan 29, 2017
Messages
7
Hi,

I'm not too experienced with VBA so resort to lots of Google searching and trial and error. I am trying to have Excel create one email for each Worksheet that has an email address in cell L2. Currently my macro successfully creates an email, attaches each worksheet as a workbook, and populates the to field with the email address in L2.

I am trying to tweak the code so that rather than attach a file, the range of A1: J (last row with data) is copy and pasted into the body of the email. Any help would be greatly appreciated.

Code:
Dim sh As Worksheet


    Dim wb As Workbook
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object




    
    TempFilePath = Environ$("temp") & "\"


    If Val(Application.Version) < 12 Then
        'You use Excel 97-2003
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        'You use Excel 2007-2016
        FileExtStr = ".xls": FileFormatNum = 52
    End If


    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    Set OutApp = CreateObject("Outlook.Application")


    For Each sh In ThisWorkbook.Worksheets
        If sh.Range("L2").Value Like "?*@?*.?*" Then


            sh.Copy
            Set wb = ActiveWorkbook


            TempFileName = "Sheet " & sh.Name & " of " _
                         & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")


            Set OutMail = OutApp.CreateItem(0)


            With wb
                .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum


                On Error Resume Next
                With OutMail
                    .To = sh.Range("L2").Value
                    .cc = ""
                    .BCC = ""
                    .Subject = Date & " " & " Allocations"
                    .Body = "Hello"
                    .Attachments.Add wb.FullName
                    'You can add other files also like this
                    '.Attachments.Add ("C:\test.txt")
                    .Save  'or use .Display
                End With
                On Error GoTo 0


                .Close savechanges:=False
            End With
            
            Set OutMail = Nothing


            Kill TempFilePath & TempFileName & FileExtStr


        End If
    Next sh


    Set OutApp = Nothing


    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
philk228,

Hello and Welcome to the Forum.
Try this on a copy of your workbook -

Between these lines of your code:
Code:
            Set OutMail = OutApp.CreateItem(0)

                   'Put the code here 

            With wb

Place the following code where shown above:
Code:
 ' Compose the message
Dim LastRow, col, rw As Long
Dim Msg as String

LastRow =Range("J" & Rows.Count).End(xlUp).Row

Msg = ""        'Clears any Previous Message

For rw =  1 to LastRow
     for col = 1 to  10     'Columns A to J
          Msg = Msg & cells(rw,col)
          'Msg = Msg & " " & cells(rw,col)     'Alternatively, The " " adds a space between cell values if desired
    Next col

    Msg = Msg & vbCrLf     'Moves cell values to next row. You can add an extra '& vbCrLf' to put a space between rows
 next rw

Then change your code:
Code:
                    .Body = "Hello"

To this:
Code:
                    .Body = Msg
Then save your workbook and make a copy of your workbook to try out this code.
Perpa
 
Upvote 0
Hello Perpa,

Thank you for the response. The information now flows into the body of the email so thank you.
Is there any way to have the information appear as a table rather than as just text?
Basically I'm trying to mimic just a straight copy and paste from an excel workbook into an email (no paste values or anything).

Thanks again for the help,
Phil
 
Upvote 0
Hi Perpa,
I was able to use the information you provided and some info from Ron de Bruin to help piece it all together.
Thanks again!
 
Upvote 0
Hi Perpa,
I was able to use the information you provided and some info from Ron de Bruin to help piece it all together.
Thanks again!

Phil,
I am glad you were able to get where you wanted to be and that the information I furnished was helpful.
You are quite welcome.
Perpa
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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