Browse » Home
Sunday, October 14, 2012
How to Use VBA to Save in Excel As a PDF
1. Double-click the Excel file name from My Documents or Windows Explorer, or launch Excel and use the 'Open' command from the start button. Browse to your file and click 'Open.'
2. Press the 'Alt' and 'F11' keys simultaneously to open the Visual Basic editor. Use the Project Explorer to browse to the module where you need to automate saving the spreadsheet as a PDF file. Place your cursor in your Visual Basic code where you want the save to occur.
3. Use the 'ExportAsFixedFormat' function to perform a 'Save to PDF.' This function can be invoked from any worksheet (e.g., ActiveSheet.ExportAsFixedFormat) or for the workbook as a whole (e.g., ActiveWorkbook.ExportAsFixedFormat).You can set a number of options with this command; the option values shown below are Excel's default values.[Mandatory] Type:= xlTypePDF[Mandatory] Filename:=
[Optional] Quality:= xlQualityStandard (xlQualityStandard or xlQualityMinimum)[Optional] IncludeDocProperties:= True ('True' means that the document properties, such as author name and document title, will be included in the PDF file; 'False' means that these properties will not be set in the PDF file.)[Optional] IgnorePrintAreas:= False ('True' means that all content in the spreadsheet should be included; 'False' indicates that only the information within your set printing areas will be included.)[Optional] From:= 1 (This is the page number where Excel should start the save. If this is omitted, then Excel begins at the first page.)[Optional] To:= 5 (This is the page number where Excel should finish the save. If this is omitted, then Excel ends on the last page.)[Optional] OpenAfterPublish:= False ('True' indicates that Excel should launch your default reader for PDF files with the PDF file open; 'False' indicates that the new PDF file should not be automatically opened.)
4. Execute the code function to ensure that the 'Save to PDF' works properly. Save the spreadsheet file.