• 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

VBA help to email printed pagebreak to an email address|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA help to email printed pagebreak to an email address|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA help to email printed pagebreak…
sp_PrintTopic sp_TopicIcon
VBA help to email printed pagebreak to an email address
Avatar
Arun J

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
December 25, 2018
sp_UserOfflineSmall Offline
1
December 25, 2018 - 8:50 am
sp_Permalink sp_Print

Hi,

I need help with the following VBA, At the minute the VBA can print page break to pdf and name them according to the name found in cell A and has loop to pint all page breaks as pdf. I was wondering if there' was a way, it could also email the pdfs generated to individual email address. I have roughly 20 page breaks and all those need to be emailed to different email addresses according to the names.

 

Sub exportPages()

Set Sht = Worksheets("Sheet1")
' The variable Sht now holds the sheet that should be printed, so we can use that in the macro instead of repeating Worksheets("Sheet1")
ExportDir = "C:\Users\Desktop\"
' A string/text with the export directory, should end with a \
NrPages = Sht.HPageBreaks.Count + 1
' Using the Sht object, count the number of horizontal page breaks and add one to know the number of pages

For p = 1 To NrPages
' Loop though the pages, actually the loop is only there as a kind of "counter"
If p = 1 Then
RwStart = 1
' The first page starts at the top and has no page break before it, so the row with your name is 1
Else
RwStart = Sht.HPageBreaks(p - 1).Location.Row
' Page 2 starts after Pagebreak 1, this code finds the row of the page break (=the row directly after it)
End If

FoundName = Sht.Range("A" & RwStart).Value
ExportName = FoundName & p & ".pdf"
' Create a variable with the name in the sheet

Sht.ExportAsFixedFormat Type:=xlTypePDF, filename:=ExportDir & ExportName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, From:=p, to:=p, OpenAfterPublish:=False
' Export the current page with the current name to the default location
Next

Set Sht = Nothing
' Clean up variables

End Sub

sp_AnswersTopicSeeAnswer See Answer
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
December 25, 2018 - 9:35 am
sp_Permalink sp_Print

Hi Arun,

the code for emailing PDF's here should help you

https://www.myonlinetraininghu.....th-outlook

Regards

Phil

Avatar
Arun J

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
December 25, 2018
sp_UserOfflineSmall Offline
3
December 25, 2018 - 11:05 am
sp_Permalink sp_Print

Hi Phil, 

Thanks for your reply, I am new to VBA, is there a chance you could help me modify the above code?  I can add the email address in one of the cells in each page break so VBA picks up the email address for that particular pdf?

Thanks in advance,

Kind Regards,

Arun 

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
December 27, 2018 - 1:07 pm
sp_Permalink sp_Print

Hi Arun,

Please see attached workbook.  This sends email using Outlook so you'll need that installed.  Emails are sent 'silently' - they aren't displayed before being sent, but you can change this if you want.  So you'll have to check your Sent Items to see what was sent.

Your code picks up the value in the top most cell of the page e.g. A1, so I am using the next cell to the right e.g. B1 for the email address.  You can change this to whatever you like.

The line that controls where the email is picked up is 

Email_To = Sht.Range("A" & RwStart).Offset(0, 1).Value

The Offset is what actually references the cell with the email address.  It sets Email_To to whatever is in the cell 0 rows down and 1 column across from Sht.range("A"&RwStart)

Tip : You should always use Option Explicit when writing code https://www.myonlinetraininghu.....ugging-vba

Cheers

Phil

sp_AnswersTopicAnswer
Answers Post
Avatar
Arun J

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
December 25, 2018
sp_UserOfflineSmall Offline
5
December 28, 2018 - 1:56 pm
sp_Permalink sp_Print

Hi Philip, 

Brilliant, that works like a charm.

Thank you so much for your help, you are a STAR.

Kind Regards,

Arun

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
December 28, 2018 - 2:18 pm
sp_Permalink sp_Print

No worries 🙂

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Nada Perovic, Denise Lloyd
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Raj Mattoo
Mark Luke
terimeri dooriyan
Jack Aston
AndyC
Denise Lloyd
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Forum Stats:
Groups: 3
Forums: 24
Topics: 6219
Posts: 27276

 

Member Stats:
Guest Posters: 49
Members: 31903
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

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.