+ Reply to Thread
Results 1 to 41 of 41

Save some pages as PDF

  1. #1
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Save some pages as PDF

    Hi all,

    I'm creating a file which will have more than 50 reports (1 report equal to 1 sheet).

    However, it isn't always necessary to save or print all the reports.

    Because of that, i thought about to create a menu where I can choose the report (or reports) to be save as PDF.

    I'm looking for a vba code that allows me to choose which report I want to save as PDF. I can choose only a report or more than one, which should be saved in just one PDF.

    It would be great if you could help me on this.

    I've seen some examples on the internet, but I can't apply them to my file.

    Thanks in advance.

    Regards,

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Create a Userform with a Listbox that lists all worksheet names. Set MultiSelect to True.
    Loop through list and put selected sheets in Array and pass that array to export function.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Thanks for your help but I don't have VBA knowledge do do that.

    I know how to create the userform, but only that.

    Regards,

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    OK, see if I can put something together later today.

  5. #5
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    I've an example of the userform but I don't know how to attached it here

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    Have a look at this. One of the esteemed members, I don't know who any more, published this way back when.
    Play around with it and see if it is something you want.
    Attached Files Attached Files

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    This is more then enough for your purposes.
    Create userform with multiselect listbox and a commandbutton, then put code in userform module.
    Don't forget to adjust filepath/name at the end of commandbutton code.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Quote Originally Posted by jolivanes View Post
    Have a look at this. One of the esteemed members, I don't know who any more, published this way back when.
    Play around with it and see if it is something you want.
    This is what I had in mind but the attached file has errors.

    The only difference I have in mind, is the possibility to choose the direction to save the file.
    Sub Salvar_PDF()

    Application.ScreenUpdating = False

    If Range("Cliente!AQ5") = 1 Then

    Sheets("MenuInicial").Select

    ThisWorkbook.Sheets(Array("Capa", "Cliente", "ResumoRn1", "Rn1", "TribAut", "Q07F", "Q08", "Q10F", "PEC", "Benefícios")).Select
    Dim vPDFPath As Variant

    Do

    bRestart = False
    vPDFPath = Application.GetSaveAsFilename(, "PDF Files (*.pdf), *.pdf")
    If CStr(vPDFPath) = "False" Then
    Exit Sub
    Else
    lAppSep = InStrRev(vPDFPath, Application.PathSeparator)
    End If

    If UCase(Dir(vPDFPath)) = UCase(Right(vPDFPath, Len(vPDFPath) - lAppSep)) Then
    Select Case MsgBox("O arquivo já existe. Deseja substituí-lo?", _
    vbYesNoCancel, "ATENÇÃO!")
    Case vbYes
    Kill vPDFPath
    Case vbNo
    bRestart = True
    Case vbCancel
    Sheets("MenuInicial").Select
    Exit Sub
    End Select
    End If
    Loop Until bRestart = False
    ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=vPDFPath, _
    OpenAfterPublish:=True

    Sheets("MenuInicial").Select

    MsgBox "Ficheiro Criado com Sucesso"

    End If
    End sub

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    Re: "has errors"
    OK, that happens every so once in a while. However, that statement does not tell us anything. It would be nice to know what the error is.
    Did you change cell C1?

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    This attachment will let you choose a folder to save in.
    Remember, it is a very rough example and needs a lot of cleaning up yet. It is just for you to see if you want it or not.
    bakerman2's example might be better. You can insert the FolderPicker in his code if you want.
    Attached Files Attached Files

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    @ jolivanes

    'Warme bakkertje' in your code ?

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    @bakerman2
    Kan heel goed mogelijk zijn.
    Could be possible. Really don't know any more where I got that from otherwise I would have mentioned it. The last thing I need is people asking me questions too difficult for me to answer/explain.
    Do you have that one tucked away somewhere also? If it is from warme bakkertje, I should, and hereby will, thank him/her for all the code I copied and used from him/her.

    Note: I think I see now what you meant. I checked the code again and saw him/her mentioned!!!!!
    Last edited by jolivanes; 09-20-2016 at 02:44 PM. Reason: add info

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Since bakerman2 and Warme Bakkertje are one and the same person I have indeed tucked it away somewhere in my archives.

  14. #14
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    Aha. Trying to confuse me.
    Is easy to do isn't it!!!
    Brood of banket?

    How are things in the "Follies Bergeres"?
    I don't know if you remember Joop van de Marel. He sang that song.

  15. #15
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Thanks for your help.

    It does almost what I want but I found 2 problems:

    The listbox includes all sheets of the workbook (some are not supposed to be printable)

    I always use sheets with simple names and with this code, they will appear also simple (Ex: R1 should be Report 1)

    Probably it is very hard to do, and I don't want to give you more work.

    Thanks anyway.

    Regards,

  16. #16
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Save some pages as PDF

    There are several ways to do it. Here I show a range in Sheet4 that can be changed to suit. Change the other routines to suit as well.
    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    The listbox includes all sheets of the workbook (some are not supposed to be printable).
    I must have overlooked that where you mentioned that.
    But you forgot to mention which sheets are not to be printed. Maybe you can enlighten us.

    Does it matter if they are "simple names"? You don't have to type the names in after all. They appear magically in the listbox but if you want it different, all you have to do is let us know which difficult sheet names have to be changed to their more simple names.

  18. #18
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    Change the UserForm_Initialize code to:
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Hi all,

    Kenneth Hobson I don't understand your code, sorry.

    Jolivanes you did it very well, thanks.

    In respect of my previous email, see below an example:

    Sheet "Blad1" should appear as "Financial Report" on the userform.
    Sheet "Blad2" should appear as "Corporate Tax" on the userform.

    (...) etc.

    The other issue I would like to solve is the possibility to choose the name of the PDF file.

    Thanks again for all your help.

  20. #20
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    You have to put that code and UserForm in your workbook.
    Like I mentioned previously, it needs cleaning. We do not know yet which way you want to go. You have been supplied with all kind of different possibilities.
    One way is to save a copy to your desktop, or wherever you can find it, and import it into your workbook.
    If you Google "excel copy userform to another workbook" you can take a pick that suits you best.

    If you can't get it to work, attach your workbook. Make sure it does not have any sensitive/personal information in it.
    Last edited by jolivanes; 09-21-2016 at 12:17 PM. Reason: Additional Info

  21. #21
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Thanks jolivanes.

    I've prepared an example but I can't find the button to attach it.

    Could you help me please?

    Thank you.

  22. #22
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    Take your pick from some of the examples here.

    https://support.office.com/en-us/art...3-50e645fe3155


    Or you can copy all the sheets from your workbook into one of the attachments that has everything in it.
    Just delete/clear some of the garbage in the first sheet and add the name of that first sheet to the exemptions.

  23. #23
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    To attach sample workbook.
    1. Click Go advanced
    2. Scroll down to Manage attachments
    3. Upper left corner click Choose file
    4. Click Upload
    5. Click Close this window
    6. Click Submit reply

  24. #24
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,527

    Re: Save some pages as PDF

    I like bakerman2's approach. A lot simpler and it does the job.
    I hope bakerman2 does not take offence that I worked with his code and changed it to suit.

    You could:
    Delete all sheets from this file except Mastersheet
    Copy all your sheets into this workbook
    Change the sheets to be exempt in the code for UserForm2
    Change the reference to where you would want the PDF files saved
    Attached Files Attached Files

  25. #25
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Ok, thanks.

    Attached is an example.

    Sheet names different of the checkbox.

    You can use listbox, for me will not be a problem.

    I just don't know how to associate a sheet to a checkbox.

    Thanks
    Attached Files Attached Files

  26. #26
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    OK, now we're getting somewhere.
    Test example file.
    Attached Files Attached Files

  27. #27
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Wow! You're almost there. Many thanks.

    I've updated the code to have the possibility to choose the file name and to close the userform at the end.

    I've now the following erros:

    1st If I don't choose any "report"
    2nd If I choose a report and then cancel or exit

    See attached.

    Thanks
    Attached Files Attached Files

  28. #28
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    That should do it.
    Attached Files Attached Files

  29. #29
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Tks bakerman2.

    1st is solved, but would be great if appear a msgbox saying "No item selected" like in your first code.
    2nd is still creating a pdf file named "FALSO" on my documents box

    Furthermore, I tryed to create new sheets and new checkbox and I can't associate them.
    Some sheets will have a checkbox button, others will not.

    Thanks

  30. #30
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Now I understand how to add new sheets. Was missing to change the Tag.

    The other issues I will wait for your expert help.

    Thanks

  31. #31
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Substitute this line
    Please Login or Register  to view this content.
    with

    Please Login or Register  to view this content.
    The second error I just can't reproduce.

  32. #32
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Thanks bakerman2.

    I still don't knoe the solution for the 2nd issue.

    The rest of the code is perfect.

  33. #33
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Describe the exact steps you take to come to the second error because I looped through the code using F8 for Yes, No, Cancel and Exit and the code does exactly what it's supposed to do.

  34. #34
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    1. Sava as PDF
    2. Choose at least 1 report
    3. Save Selected as PDF
    4. Cancel

    Still creating a pdf file "FALSO"

  35. #35
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Aaaah, you cancel when having to choose a name.
    Change this line
    Please Login or Register  to view this content.
    to this

    Please Login or Register  to view this content.

  36. #36
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Hi,

    Now, i can cancel with no error but i can't go forward and save as pdf...

    Tks

  37. #37
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Logical no ?????
    If you don't choose a name for your PDF-file you can't proceed.

  38. #38
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20

    Re: Save some pages as PDF

    Sorry, I'm bad in English and sometimes is difficult to explain me.

    I've changed the code following your previous post.

    1st problem was solved. Now, when I cancel, it works perfect. However, if I decide not to cancel, and choose a name/location to the file, appears an error on that line you told me to change.

  39. #39
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Declare vPDFPath as Variant instead of String.

  40. #40
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Save some pages as PDF

    Thanks for adding rep points and I assume all works as intended now?

  41. #41
    Registered User
    Join Date
    09-20-2016
    Location
    Lisbon, Portugal
    MS-Off Ver
    2013
    Posts
    20
    Quote Originally Posted by bakerman2 View Post
    Thanks for adding rep points and I assume all works as intended now?
    Yes, it's perfect. It will be very useful. Thanks for all

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Save only certain pages in workbook template
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-04-2015, 07:24 AM
  2. Save only certain pages in workbook template
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-03-2015, 12:03 PM
  3. Save to .pdf over multiple pages?
    By JungleJme in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2013, 04:26 AM
  4. Extract pages from word file and save extracted pages to new word file
    By bonny24tycoon in forum Word Programming / VBA / Macros
    Replies: 3
    Last Post: 06-28-2012, 09:45 AM
  5. VBA to save URL pages
    By Mth1jdc2008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-29-2008, 01:14 PM
  6. [SOLVED] How to d/load & save web pages w/VBA?
    By Ron in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-13-2006, 03:10 PM
  7. [SOLVED] How do you only save 4 pages of the workbook
    By Cathy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-01-2005, 04:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1