+ Reply to Thread
Results 1 to 14 of 14

Creating a PDF file from Excel using VBA code

  1. #1
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Creating a PDF file from Excel using VBA code

    Hi All,

    I need some help to be able to accomplish the following:

    I have 150+ reports in a folder. Each report has multiple tabs. All I want to be able to do is create a PDF from the first tab of each report and save it as a file in a sub-folder. The PDF file should be named using the file name of each report. The first tab of each report is named “Dashboard”

    Could someone please help me with a code that can help me accomplish this. I searched on Google, but couldn’t find something fitting my needs.

    My first tab has some data and about 5 to 7 graphs.

    Please assist.
    In your greatness, remain humble!

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    Perhaps a macro like this.

    Please Login or Register  to view this content.
    You must change the sPath to suit your needs and also this part of the code "I:\Temp\" to save the file to the preffered subfolder. When changing these parameters don't forget the last "\" in the commands i.e. "C\Temp\" will work but not "C:\Temp"

    Alf

  3. #3
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Re: Creating a PDF file from Excel using VBA code

    Wonderful. Thanks a bunch. I'll try it and get back shortly.

    Quick question: This will loop through each file and create individual files, right? Just checking as am driving now. Saw your reply so stooped by to convey thank you!

    I'll test it once I get back to office.

    Kind regards,
    S

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    Yes, macro will loop through all ".xlsx" files in given folder ("sPath") activate sheet "Dashbord" and making a pdf file with the same name as the ".xlsx" file (- the .xlsx part).

    The only thing I'm unsure about is how the graph part behave when converting to pdf.

    Alf

  5. #5
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Re: Creating a PDF file from Excel using VBA code

    Quote Originally Posted by Alf View Post

    The only thing I'm unsure about is how the graph part behave when converting to pdf.

    Alf

    Dear Alf,

    This works like a charm! Just one thing though, is it possible make it somewhat dynamic so that it auto fits the contents? In other words, in some cases the partial graph / table is getting captured in one PDF, and the remaining in the next page. I would like the complete table or graph in one PDF sheet.

    Please advise.

    Thanks,
    Spi

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    First let me correct two mistakes I made writing this macro:

    Change
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    because you have to set action to take before error is found and the correct way to write this statement is "On Error GoTo". This error trapping is for xlsx files that have no "Dashboard" sheet, they should be ignored and when this error occurce macro should jump down to line "skipper:"

    But if you excamine the macro you will see first the file is opened, then sheets("Dashbord") should be activated, if there is no "Dashboard" this trows an error and macro jumps dovn to line "skipper:" but the way it's written the "skipper:" line is placed after the "file close command" so this means the file will stay open untill you manually close it.

    Therfore change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    in some cases the partial graph / table is getting captured in one PDF, and the remaining in the next page.
    I'll have a go at it but it would be easier for me if you could upload one of the xlsx files that spreads ower two pdf pages so I could play around with it.

    Alf

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    Ok I found a way to fit the result to a one page pdf file, but testing I fount that the result greatly depends of how many pages the excel worksheet will create as it is converted to a pdf file. I would assume 3 to 4 pages should work fine anything more than that may be difficult to read.

    There is a freebee called "FreePDF Creator" that perhaps could suit your needs, there is also a bit information on why the pdf sheets created from excel looks the way they do as excel uses "Standard settings"

    http://www.pdfconverter.com/resource...o-one-page-pdf

    The updated code looks like this now:

    Please Login or Register  to view this content.

    As this should solve your problem could you please mark your thread "Solved"

    Alf

    Ps
    To mark your thread solved do the following:
    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved

  8. #8
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Re: Creating a PDF file from Excel using VBA code

    [QUOTE

    As this should solve your problem could you please mark your thread "Solved"

    Alf

    Ps[/QUOTE]

    Fantastic solution. Your Macro does it all. I really don't need a free converter. It is magical. I processed 273 files exactly! It worked beautifully, and with tremendous speed. Just a small tweak may be needed and I'll be happy to close the thread. What it does is in the last graph, it doesn't take it completely else everything fits perfectly. Do you think there is a way that tweak can be made in the code to read it completely?

    Best,
    Spi

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    As I've not see the file (s) that makes this problem it's difficult to tell. You could try to put problem file (s) in a sparate folder and run the first macro to see if you get everything converted to a proper pdf fil.

    Since macro works for most of the files I suspect it has to do with layout or worksheet size.

    Upload one of the files that works and one that don't and I'll see what I can do.

    Alf

  10. #10
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Re: Creating a PDF file from Excel using VBA code

    Quote Originally Posted by Alf View Post

    Upload one of the files that works and one that don't and I'll see what I can do.

    Alf
    Thanks Alf. I completely understand. let me try what you've suggested, and then I'll post the problem files if it doesn't work still. Thanks much!

    Best,

    Spi

  11. #11
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Re: Creating a PDF file from Excel using VBA code

    Quote Originally Posted by Alf View Post

    As this should solve your problem could you please mark your thread "Solved"

    Alf

    Ps
    Dear Alf,

    First and foremost, please accept my apologies for taking a little more time to check this out. I wanted to be absolutely sure. It turned out to be an issue with my data layout, as you'd mentioned. I took sometime to fix it, and everthing now seems to per working as expected.

    Please accept a sincere thank you. Your solution is just "PERFECT". It has helped me execute my task with speed and accuracy. I'm oblidged. Thank you for your magical solution.

    I'm closing the thread as "Solved". I can't do anything else.

    Best,
    Spi

  12. #12
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    You are welcome.

    Thanks for feedback and rep

    Glad this worked for you, but still I do wonder because the the format setting to one page is done in the excel environment, then the page is saved as a pdf file with minimal settings. One would think that the proper way to do this would be to add the "one page" setting in the pdf environment during the "save as pdf" operation.

    Alf

  13. #13
    Forum Contributor spiwere's Avatar
    Join Date
    01-11-2013
    Location
    IN
    MS-Off Ver
    Excel 2007
    Posts
    410

    Re: Creating a PDF file from Excel using VBA code

    Quote Originally Posted by Alf View Post
    You are welcome.

    Thanks for feedback and rep

    Glad this worked for you, but still I do wonder because the the format setting to one page is done in the excel environment, then the page is saved as a pdf file with minimal settings. One would think that the proper way to do this would be to add the "one page" setting in the pdf environment during the "save as pdf" operation.

    Alf
    Dear Alf,

    I did not see any issues nor any of my team members using it have pointed any out, just in case we do will it be okay to comeback and post the problem file?

    Thanks,
    Spi

  14. #14
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Creating a PDF file from Excel using VBA code

    will it be okay to comeback and post the problem file?
    No problem, since I'm a subscriber to this thread and will be for a year at least so any posting done in this thread will send an email to me notifying me of any activity.

    Alf

+ 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. Why is my VBA code creating a file in the Documents folder?
    By hadamhiram in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2015, 06:57 PM
  2. Creating Code to open a file whose name changes by week
    By dafigu01 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-04-2014, 06:55 PM
  3. Converting excel 2003 file ext macro file-save code to reflect excel 2007 exts
    By pmanoloff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2013, 12:17 PM
  4. Creating a .txt file extracting information from a Excel file saving as .resx file
    By AbdallahHajbed in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-19-2012, 09:00 AM
  5. Creating a new excel file from an existing file using macro
    By Macro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2005, 02:06 PM
  6. Seting a Constant (via code) when creating a new file based on a template
    By Conan Kelly in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-15-2005, 01:05 PM
  7. Import VBA Code in Excel-File ? (Export VBA Code to file)
    By Matthias Pospiech in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2005, 01:06 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