Macro to email Range from Excel Sheet

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to email a range which I obtain from Ron de Bruin's Website

i have made a few small changes

I would be appreciateed if someone can mend it to display so the email can be checked before clicking on Send button on outlook

Code:
 Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
'Working in Excel 2002-2016
    Dim AWorksheet As Worksheet
    Dim Sendrng As Range
    Dim rng As Range

    On Error GoTo StopMacro

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

    'Fill in the Worksheet/range you want to mail
    'Note: if you use one cell it will send the whole worksheet
    Set Sendrng = Worksheets("Sheet1").Range("B1:B15")

    'Remember the activesheet
    Set AWorksheet = ActiveSheet

    With Sendrng

        ' Select the worksheet with the range you want to send
        .Parent.Select

        'Remember the ActiveCell on that worksheet
        Set rng = ActiveCell

        'Select the range you want to mail
        .Select

        ' Create the mail and send it
        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope

            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = "This is test mail 2."

            With .Item
                .To = Sheets(1).Range("A1")
                .CC = ""
                .BCC = ""
                .Subject = Sheets(1).Range("B1")
                .Send
            End With

        End With

        'select the original ActiveCell
        rng.Select
    End With

    'Activate the sheet that was active before you run the macro
    AWorksheet.Select

StopMacro:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Change the following :

VBA Code:
With .Item
                .To = Sheets(1).Range("A1")
                .CC = ""
                .BCC = ""
                .Subject = Sheets(1).Range("B1")
               
                'change the following from .Send to .Display
                'then either delete or comment out .Send
                
                 .Send
               
            End With
 
Upvote 0
Thanks for the reply

I amend code to from .send to .Display but email not being displayed

Not sure why it is not being displayed , unless some of the other code needs to be amended as well

With .Item
.To = Sheets(1).Range("A1")
.CC = ""
.BCC = ""
.Subject = Sheets(1).Range("B1")

'change the following from .Send to .Display
'then either delete or comment out .Send

.Display [/code]

Kindly check & advise
 
Upvote 0
VBA Code:
Option Explicit


Sub Send_Mail_From_Excel()
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim Sendrng As Range
    
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Fill in the Worksheet/range you want to mail
    'Note: if you use one cell it will send the whole worksheet
    Set Sendrng = Worksheets("Sheet1").Range("B1:B15")

    'Send Mass Email Using Excel VBA Macro Code
    With OutlookMail
        .To = Sheets(1).Range("A1").Value
        .CC = ""
        .BCC = ""
        .Subject = Sheets(1).Range("B1").Value
        '.Send
        .Display
    End With
 
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Upvote 0
VBA Code:
Option Explicit

Sub Send_Mail_From_Excel()
    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim Sendrng As Range
    
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Fill in the Worksheet/range you want to mail
    'Note: if you use one cell it will send the whole worksheet
    Set Sendrng = Worksheets("Sheet1").Range("B1:B15")

    'Send Mass Email Using Excel VBA Macro Code
    With OutlookMail
        .To = Sheets(1).Range("A1").Value
        .CC = ""
        .BCC = ""
        .Subject = Sheets(1).Range("B1").Value
        .Body = Sheets(1).Range("B3").Value
        '.Send
        .Display
    End With
 
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Upvote 0
Many Thanksfor the help Logit

I can now see where there problem was

I did not have the code below which is required to generate text in the body of the email. Don't know how I overlooked this

Code:
 .Body = Sheets(1).Range("B3").Value
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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