+ Reply to Thread
Results 1 to 21 of 21

Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

  1. #1
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi excelforum Experts,

    I have a separate excel macro workbook that contains two buttons named "Consolidate" and "Print PDF" respectively. I would like to focus on the "Print PDF" button since the other button works fine though it is not incorporated into the excel macro that I have mentioned or as attached.

    I am having an error upon running the macro as shown on screenshot below and I could not find what's wrong with the code or I might overlooked some codes here. I appreciate if there could be someone that would help me figure these things out. Please see screenshots below for reference:

    Run-time Error.png

    Working VBA Code.png

    Here are the factors that I need to consider in order to arrived at my desired output. Please see also attached workbooks for reference:

    1.) The "Print PDF" button will work on the other open/ active workbook. This workbook is an output of my "Consolidated" button.
    2.) Each worksheet has fluctuating number of rows and columns, so I need to Set Print Areas ALL worksheets before converting to PDF.
    3.) Each worksheet name will be used as file name and all worksheets will be saved to my desired file path.


    Warmest regards,
    Arnel
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    The 1st question, why you get error when PDF'ing the sheets is that the code wants to PDF the hidden sheets as well. When I unhid your sheets, the code does not error. If you want to just pdf the visible sheets, you will have to tell the code that.

    You can also pdf all the visible sheets by changing `activesheet` to `activeworkbook`

    ActiveWorkbook.ExportAsFixedFormat

    The code to loop through the sheets and set the printrange

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi davesexcel,

    Thanks for taking time answering my query.

    I am sorry if I made you confused but what I am trying to print into PDF after setting the print range is the "Raw Data" workbook which contains five(5) worksheets. The other workbook named "Print to PDF Generator" is the one I needed to run the macro and this should not be formatted nor be printed.

    I tried running the macro adding the code that you have provided but I am still getting the same error as shown on screenshots on my previous post. I appreciate if you could please help me figure out and fix the missing pieces. Thank you!

    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    As long as Raw Data is the active workbook when running code there's no problem.
    But since you're running from another workbook try this.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 10-19-2017 at 02:03 AM. Reason: Corrected code
    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.

  5. #5
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi bakerman2,

    Thanks for your immediate feedback!

    I replaced the existing codes that I have with the codes that you provided but I am getting an error shown on screenshot below.

    Run-time Error.png

    Working VBA Code.png

    I appreciate if you could please help correct these codes for me to come up with my desired output. Thank you so much!

    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Sorry my bad.

    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    Last edited by bakerman2; 10-19-2017 at 02:00 AM.

  7. #7
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi bakerman2,

    Thanks for your feedback!

    I already corrected the code but still I am getting an error. There were set of codes that were highlighted in yellow after clicking on the "debug" button that appears on my screen. Please see screenshot below:

    Run-time Error.png

    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    My brain was tired yesterday I guess.
    Sorry for confusion but this is definitly the correct one.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi bakerman2,

    I am sorry for the inconvenience and thank you for taking the time in helping me resolve this issue. However, an error keeps on appearing as shown on screenshot below:

    Run-time Error.png

    I tried researching online with regards to the same error but I could not find any solutions yet. Does the below applications affect the existing codes we have?

    1.) MS Excel 2007
    2.) CutePDF Writer

    I appreciate your help. Thanks!


    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    I also have XL2007 and it works flawless.
    If you walk through the code line by line (F8) what value does MyFolder have after you have selected a name ?
    Is that a valid path ?
    Also you don't need to set Active printer.

  11. #11
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Quote Originally Posted by arnel_10 View Post
    Hi bakerman2,

    I am sorry for the inconvenience and thank you for taking the time in helping me resolve this issue. However, an error keeps on appearing as shown on screenshot below:

    Attachment 543598

    I tried researching online with regards to the same error but I could not find any solutions yet. Does the below applications affect the existing codes we have?

    1.) MS Excel 2007
    2.) CutePDF Writer

    I appreciate your help. Thanks!


    Warmest regards,
    Arnel
    Hi Arnell,
    What is the error?

  12. #12
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi davesexcel,

    Thanks for coming back!

    Please refer to the screenshot below for the error that I am getting:

    Run-time Error.png

    Please Login or Register  to view this content.
    The codes above were highlighted in yellow after clicking the "debug" option.

    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    How about this ?

    Please Login or Register  to view this content.
    Last edited by bakerman2; 10-20-2017 at 07:41 PM.

  14. #14
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi bakerman2,

    I am attaching the workbooks that I am working with for your reference.

    It looks like the error comes from the "ExportAsFixedFormat" portion.

    I would like to refresh the flow of the macro that I am going to accomplish with the help of you. After clicking the Print PDF button from the Print to PDF Generator workbook, the macro should run with the following steps:

    1.) Go to the "Raw Data" workbook and Set Print Area of each worksheet (I always have 10-50 worksheets with fluctuating no. of rows and columns).
    2.) Print each worksheet as PDF.
    3.) Save each worksheet on my desired folder which will be at the range C6 of Print to PDF Generator workbook.
    4.) Save and Close the Print to PDF Generator workbook.
    5.) Open the location of the folder which contain the PDF files (output).

    I am going to review all the codes that you have provided and will take notes. This will help me understand the process that can be used in the future.

    Thanks for your patience and kindness.

    Warmest regards,
    Arnel
    Attached Files Attached Files

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Let's start with this. This prints every sheet in the desired folder with sheetname as filename.

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hi bakerman2,

    I am still getting an error in the line shown below:

    Please Login or Register  to view this content.
    By the way, Range("C6") is on the "Print to PDF Generator" workbook. We may need to activate this workbook after the other one (i.e. "Raw Data" workbook). Thanks!

    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    If it's a valid path in C6 and workbook Raw Data is open when running code then I can't do much for you anymore.
    When I run code it creates all 6 sheets in the designated path. (XL2007)
    I really have no clue anymore why it's not working with you.
    Last edited by bakerman2; 10-21-2017 at 01:32 AM.

  18. #18
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    It worked for me, although I had to change the path location in C6

  19. #19
    Registered User
    Join Date
    03-01-2017
    Location
    Manila, Philippines
    MS-Off Ver
    MS Office 2007
    Posts
    77

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    Hello there,

    I am glad to inform you that the codes that you both provided worked fine upon testing with the other computer. I am assuming that it was because of the PDF application that was installed on my computer which was CutePDF. I tried testing the code with the other computer which was using the Bullzip PDF and it worked well.

    For the sake of the future reader of this thread, may I know what PDF applications you both are using that worked fine? I will try to use the codes with the other PDF applications that will not be mentioned on this thread and I will find out what applications will worked well and will not.

    By the way, I bumped into the website shown below and found some relevant information with regard to this thread. This could be of great help.

    https://www.excelguru.ca/content.php...age=2#comments

    I will be marking this thread as "SOLVED" once I got your responses. Thank you so much for your help and efforts. Cheers!

    Warmest regards,
    Arnel

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

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    It had crossed my mind that it was interferring with your pdf software but since I don't have any 3rd party pdf software installed I couldn't verify.

    I haven't nor did I ever have such software installed or used so I can't make an opinion about it and it will be a case of trial and error I guess.

    Anyway thanks for feedback and rep+.

  21. #21
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: Excel Macro: Set Print Area and Saved as PDF (each Worksheet)

    The codes provided do not use the pdf print drivers.
    In any workbook, goto=> file=> saveas in the "Save as Type" select PDF. Since xl'07 you no longer required a PDF printer

+ 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. [SOLVED] Modify a macro that creates PDF file from the print area on a worksheet!
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-06-2017, 12:54 PM
  2. OFFSET function to define print area not being saved in name manager
    By TFiske in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 11-16-2016, 03:51 PM
  3. Replies: 18
    Last Post: 04-11-2015, 12:48 PM
  4. Setting Print Area within Excel Macro
    By RenaWare in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-29-2013, 08:10 PM
  5. Replies: 1
    Last Post: 02-12-2013, 12:16 PM
  6. Macro to duplicate a worksheet Print Area
    By RobGodfrey in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-24-2011, 06:38 AM
  7. [SOLVED] Format worksheet to view only the print area cells in Excel
    By jshoff271 in forum Excel General
    Replies: 5
    Last Post: 12-19-2005, 03:40 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