• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
You are here: Home
Lost password?
sp_Search
Advanced Search|Last Search Results
Advanced Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search

Please confirm you want to mark all posts read

Mark all topics read

sp_MobileMenu Actions
Actions
sp_LogInOut
Log In
sp_Search

Search Forums

sp_RankInfo
Ranks Information
Avatar

New/Updated Topics

General Excel Questions & Answers

  Calculate occurrences of max value by hour

  Lookup in a 3 Dimentional Table

  Amortization schedule with changing interest rate

  Exact Match searching, NOT partial.

Dashboards & Charts

  Connecting 2 separate pivot charts or data sets to calculate…

VBA & Macros

  IF and Max Formula through VBA

  VBA to Have Template file remain Open after Copy is Saved &a…

Power Query

  Using Table.ReplaceValue with Text.Combine to do an in place…

  Removing columns in Power BI source file

  How to add a new query for each cell of a column

  Creating a new table from other data sources

  Converting Values from positive to Negative in Power Query

  Running total in power query

Xtreme Pivot Tables

  Structuring data to drive pivot tables.

Power BI

  Plotting a line in a visual representing cumulate sum

Select Forum

  Rules and Guides

Forum Rules and Guides

  Public Forums - For Registered Users

General Excel Questions & Answers

Dashboards & Charts

VBA & Macros

Power Query

Power Pivot

  Course Members Only

Excel Dashboards

Power Query

Power Pivot

Xtreme Pivot Tables

Excel for Decision Making

Excel for Finance

Power BI

Excel

Word

Outlook

Excel Expert

Excel for Customer Service Professionals

Excel Analysis Toolpak

Excel Tables

Excel for Operations Management

Financial Modelling

Advanced Excel Formulas

Pivot Tables Quick Start

ForumsVBA & Macros
sp_TopicIcon
Want to send a range in a sheet with Outlook. But I want to give the user an option for attachment or copy
Avatar
robert alvarez
Posts: 16
Level 0
February 21, 2020 - 6:53 am

1

Hi

 

I have a tool in which the user clicks a macro button and it send out the range of the sheet in the body of an email in Outlook.

 

We are having an issue that sometimes the range can be over 1000 rows long.  The receiver will have to copy paste it in a workbook anyways, so I wanted to give the sender the option with a MsgBox to select if they want the range sent as an attachment or in the body of the Outlook email

 

below is the code that I have:

 

Sub Main()
 
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim lastRow As Long
 
    Set rng = Nothing
    On Error Resume Next
 
    lastRow = Sheets("Sheet1").Columns(1).Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Set rng = Sheets("Sheet1").Range("A1:F" & lastRow).SpecialCells(xlCellTypeVisible)
 
    
    On Error GoTo 0
 
    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If
 
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
 
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
 
    On Error Resume Next
    With OutMail
        .To = Sheets("Contacts").Range("F4").Value
        .CC = "isaac.santos@expeditors.com,sju-ivm@expeditors.com"
        .BCC = ""
        .Subject = "Expeditors Statement"
        .HTMLBody = RangetoHTML(rng)
        .Display 
    End With
    On Error GoTo 0
 
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
 
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Avatar
Catalin Bombea
Iasi, Romania
Posts: 1810
Level 10
February 24, 2020 - 2:15 pm

2

Hi Robert,

You can replace the line

.HTMLBody = RangetoHTML(Rng)

with:

If MsgBox("Send as attachment?", vbYesNo, "Sending Data") = vbYes Then
.Attachments.Add RangeToNewBook(Rng)
Else
.HTMLBody = RangetoHTML(Rng)
End If

And here is the function that creates the new book:

Function RangeToNewBook(ByVal Rng As Range) As String
Dim Wb As Workbook
Set Wb = Workbooks.Add
Rng.Copy Destination:=Wb.Worksheets(1).Cells(1)
Wb.SaveAs ThisWorkbook.Path & Application.PathSeparator & "Temp " & Format(Now(), "yyyymmddhhmmss") & ".xlsx"
RangeToNewBook = Wb.FullName
Wb.Close True
End Function

Forum Timezone:
Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Brian Pham, Riny van Eekelen
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)

Devices in use: Desktop (8), Phone (3)

Forum Stats:
Groups: 3
Forums: 24
Topics: 6223
Posts: 27295
Member Stats:
Guest Posters: 49
Members: 31920
Moderators: 3
Admins: 4
© Simple:Press

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.