
#Vba print selection to pdf code
So here's the code that accomplishes this:Ĭode: Select all Sub ExportActiveSheetPrintRangeToPDFĭocument = ĭispatcher = createUnoService(".DispatchHelper")
#Vba print selection to pdf pdf
And though you could simply use the outlined manual steps above each Export instead of a Macro, in my case I wanted to a) insure the user doesn't skip the "Selection" option (which would instead create a PDF for the entire Spreadsheet), or, b) I also needed to use an IF/ELSE statement in my macro for form validation before allowing the PDF to be exported (validation stuff removed from code below for clarity) You could also use a simple Macro to "Print To Pdf", which runs much faster, but Printing To PDF doesn't preserve the hidden hyperlinks like "Export To PDF" does. Many other examples out there incorrectly export the entire Spreadsheet (which is what I kept running into again and again if not allowing the Macro to write and name the file) Great code but doesn't give the normal user any control over where to save the file, or what to name the file on a per instance basis.

There are examples out there of doing this, but only if the Macro names the file and then saves it in the background with a predetermined name. What this does, if no range is currently selected, is it will export the currently Defined Print Range of only the current Active Sheet to a PDF, prompting the user for the directory and filename to save the PDF as, and prompt the user if about to overwrite an existing PDF. What I wanted was a Macro to provide what the following steps in Calc provide:Ĭhoose 'Selection' under 'Range' on the General Tab So here's the goal and solution in case anyone else can benefit from it: I couldn't find the exact solution anywhere online. I finally figured out the code for a problem of mine (subject line) after pulling out my hair out on it for a few days. So I figured I'd create an account and hopefully give back in a small way. Thank you to all of you regular posters and OO! Your work is much appreciated, even if it mostly goes unsaid! You can also move the button around while it is on the edit mode (have rectangle line around).I've gained a tremendous amount of information by trolling this board. For this example, I change it to Print as shown in picture below. Now you can change it to the name as you want. The VB code will be saved under the Module that can be used on any sheet under this workbookĪfter you done with VB code and come back the Excel Sheet, a name of the button still shows the default name given by MS Excel.
#Vba print selection to pdf how to
This How To will show the step by step below. We just need to add the VB code under the click event for this button.

Some time we need a short cut to print the current screen by using just one click. It will print an active screen on current sheet after you click on Print button. In this How To, I will show how to create a Print button to print current screen. How to Create a Print button to print a screen
