• 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
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Exporting PDF to Outlook|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Exporting PDF to Outlook|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 & MacrosExporting PDF to Outlook
sp_PrintTopic sp_TopicIcon
Exporting PDF to Outlook
Avatar
sfaulds
Member
Members
Level 0
Forum Posts: 17
Member Since:
August 24, 2017
sp_UserOfflineSmall Offline
1
August 24, 2017 - 5:31 am
sp_Permalink sp_Print

I’ve used the code from here: https://www.myonlinetraininghu.....ment-48666 - and I am getting the following error:

Run-time error ‘1004’:

Document not saved. The document may be open, or an error may have been encountered when saving.

Debug brings me here:

[code]

‘Create the PDF

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _

:=False, OpenAfterPublish:=OpenPDFAfterCreating

[/code]

I've attached my workbook.

Additionally - I am hoping to add to this where the user can use one macro to send every unhidden tab to .pdf/email. (Bonus points if we can get a pop-up selector to "check" which tabs they want to send to a single .pdf/email!!!)

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1844
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
August 24, 2017 - 3:40 pm
sp_Permalink sp_Print sp_EditHistory

Hi,

As I suspected, the file name contains illegal chars, a date was used, with forward slash separator. The date should be reformatted:

    PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Name _
                & "_" & Format(CurrentMonth, "yyyy-mm-dd") & ".pdf"

 

To print multiple sheets in the same pdf, you have to select them before running the code to create the pdf.

Use a code like this:

 Sub SendAllSheets()
 Dim Cell As Range, SheetList() As Variant, Counter As Integer
 
 Counter = 0
For Each Cell In ThisWorkbook.Worksheets("Sheet1").ListObjects(1).ListColumns(1).DataBodyRange
    If Len(Cell) = 0 Then Exit For
    If ThisWorkbook.Worksheets(Cell.Text).Visible <> xlSheetVisible Then ThisWorkbook.Worksheets(Cell.Text).Visible = xlSheetVisible
    If Cell.Offset(0, 1) Like "Yes" Then
        ReDim Preserve SheetList(1 To Counter + 1) As Variant
        SheetList(Counter + 1) = Cell.Value
        Counter = Counter + 1
    End If
Next
ThisWorkbook.Worksheets(SheetList).Select

create_and_email_pdf

ThisWorkbook.Worksheets(1).Select

'Hide sheets
For Each Cell In ThisWorkbook.Worksheets("Sheet1").ListObjects(1).ListColumns(1).DataBodyRange
    If Len(Cell) = 0 Then Exit For
    If Cell.Offset(0, 2) Like "Yes" Then ThisWorkbook.Worksheets(Cell.Text).Visible = xlSheetHidden
Next

End Sub

This code is based on the setup table from Sheet1 of the attached file, where you can set which sheets must be printed, and which should be hidden after printing to pdf.

sp_AnswersTopicAnswer
Answers Post
Avatar
sfaulds
Member
Members
Level 0
Forum Posts: 17
Member Since:
August 24, 2017
sp_UserOfflineSmall Offline
3
August 25, 2017 - 1:50 am
sp_Permalink sp_Print

Thank you Catalin! So helpful!

I am trying to use the 2nd code you sent for printing certain pages, but when i inserted it into my active workbook (of course copying "Sheet1" over, too), it gives me Run-time error '9': Subscript out of range for the following line of code:

If ThisWorkbook.Worksheets(Cell.Text).Visible <> xlSheetVisible Then ThisWorkbook.Worksheets(Cell.Text).Visible = xlSheetVisible

 

Is there something I need to do since I moved it to a slightly different workbook? The skeleton of the workbook is the same as my previous attachment, but I just have actual data feeding into the "new" one.

 

Thanks again for your help. I've been playing around with it for a while and just can't seem to figure out why/how to fix.

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1844
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
August 25, 2017 - 5:25 am
sp_Permalink sp_Print

There is also a defined name used to create the sheet list, make sure you have it in the other book, its name is... SheetList. Normally, if you copied Sheet1, the name should be transferred automatically, but you sill check the formula to see if it looks exactly the same as in the original sample.

That error you had means that there is no sheet in your book with that name from column 1, does the list of sheets from Sheet1 table look ok?

Avatar
sfaulds
Member
Members
Level 0
Forum Posts: 17
Member Since:
August 24, 2017
sp_UserOfflineSmall Offline
5
August 25, 2017 - 5:38 am
sp_Permalink sp_Print

The sheet names in column one look accurate - and I confirmed that the defined name is SheetList in my database too.

I did change the name of the tab to PrintOptions and updated in the code as well, but I had tried it both ways with the same error.

 

I've attached the exact workbook I am working with. Maybe you can help with that rather than the one missing data. 🙂

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1844
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
August 28, 2017 - 1:12 am
sp_Permalink sp_Print

Hi,

The code failed at: 8OZ GUSSRP Range Planning Grap sheet, the full name of that sheet was 8OZ GUSSRP Range Planning Graph

The source of the eror is the formula I set for PrintOptions table, I knew that the limit for sheet names is 30 chars, your sheet has 31 chars, so the formula did not return the complete sheet name in this case:

=IFERROR(MID(INDEX(SheetList,ROW(A11)),FIND("]",INDEX(SheetList,ROW(A11)))+1,30),"")

Change the formula to:

=IFERROR(MID(INDEX(SheetList,ROW(A11)),FIND("]",INDEX(SheetList,ROW(A11)))+1,50),"")

Avatar
sfaulds
Member
Members
Level 0
Forum Posts: 17
Member Since:
August 24, 2017
sp_UserOfflineSmall Offline
7
August 28, 2017 - 11:37 pm
sp_Permalink sp_Print

This did it - THANK YOU!!!! 😀

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1844
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
8
August 29, 2017 - 12:51 am
sp_Permalink sp_Print

You're welcome, glad to hear that it works as you waned.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Mynda Treacy, Eric Nyarko, Lakisha Hall, Tracy English, QSolutions Group
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
jaryszek: 183
Newest Members:
Jennifer Rodriguez-Avila
Khaled Ibrahim
Kiran Supekar
Lisa Myers
Ronald White
Ginette Guevremont
Taryn Ambrosi
Mark Davenport
Christy Nichols
Harald Endres
Forum Stats:
Groups: 3
Forums: 24
Topics: 6530
Posts: 28602

 

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

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • 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

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

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

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.