Excel Multiple Ranged Cells to Outlook

N3cTr0

New Member
Joined
Jan 6, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

I've checked a few videos even tried AI but I am struggling with this code, I want it to take 2 ranges and send it in an email, but the 2nd range always seems to override the 1st one, and thus only the 2nd table appears.

Sub SendEmail()
Dim shtSummary As Worksheet
Dim shtStats As Worksheet
Dim LastRowSummary As Long
Dim LastRowStats As Long
Dim rngSummary As Range
Dim rngStats As Range
Dim OutApp As Object
Dim OutMail As Object

' Set the summary sheet and the last row to copy
Set shtSummary = ThisWorkbook.Sheets("Summary")
LastRowSummary = shtSummary.Cells(shtSummary.Rows.Count, "I").End(xlUp).Row

' Define the summary range to copy
Set rngSummary = shtSummary.Range("I1:O" & LastRowSummary)

' Set the stats sheet and the last row to copy
Set shtStats = ThisWorkbook.Sheets("Stats")
LastRowStats = shtStats.Cells(shtStats.Rows.Count, "A").End(xlUp).Row

' Define the stats range to copy
Set rngStats = shtStats.Range("A1:Y" & LastRowStats)

' Create a new email and paste the ranges into the body
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Reports"
.Display

'Copy the summary range and paste it into the email body
rngSummary.Copy
.GetInspector.wordEditor.Range.PasteAndFormat wdTableOverwriteStyle


'I'm guessing something needs to be between the two so that they can be separated?


' Copy the stats range and paste it into the email body
rngStats.Copy
.GetInspector.wordEditor.Range.PasteAndFormat wdTableOverwriteStyle

End With

' Clear the clipboard
Application.CutCopyMode = False
End Sub
 

Attachments

  • references.png
    references.png
    16.1 KB · Views: 6

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I want it to take 2 ranges and send it in an email, but the 2nd range always seems to override the 1st one, and thus only the 2nd table appears.

Try this macro. The code also shows how to insert text before and after the pasted ranges.
VBA Code:
Public Sub Create_Email()

    Dim shtSummary As Worksheet
    Dim shtStats As Worksheet
    Dim LastRowSummary As Long
    Dim LastRowStats As Long
    Dim rngSummary As Range
    Dim rngStats As Range
    
    Dim olApp As Outlook.Application
    Dim olEmail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Word.Document

    ' Define the summary range to copy
    With ThisWorkbook.Worksheets("Summary")
        Set rngSummary = .Range("I1:O" & .Cells(.Rows.Count, "I").End(xlUp).Row)
    End With
    
    ' Define the stats range to copy
    With ThisWorkbook.Worksheets("Stats")
        Set rngStats = .Range("A1:Y" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With

    Set olApp = New Outlook.Application
    Set olEmail = olApp.CreateItem(olMailItem)

    With olEmail
        .BodyFormat = olFormatRichText
        .Display

        .To = ""
        .Subject = "Reports"

        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor

        With wdDoc.Range
            .InsertBefore "Summary range:"
            .InsertAfter vbCrLf
    
            rngSummary.Copy
            PasteAtEnd wdDoc
    
            .InsertAfter vbCrLf & vbCrLf
            .InsertAfter "Stats range:"
    
            rngStats.Copy
            PasteAtEnd wdDoc
        
            .InsertAfter vbCrLf & vbCrLf
            .InsertAfter "End of data"
        End With

    End With

End Sub

'paste from clipboard to the end of the document
Private Sub PasteAtEnd(doc As Word.Document)
    With doc
        .Content.Select
        .Application.Selection.Collapse wdCollapseEnd
        .Application.Selection.PasteAndFormat wdChartPicture
    End With
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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