Forum Discussion
Print TO PDF in VBA
To modify your VBA code to print to PDF in Excel, you can use the "ExportAsFixedFormat" method. Here's how you can adapt your code for printing to a PDF file:
For printing the active sheet as a PDF:
vba code:
Sub PRINT_PAGE_PDF()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim PDFPath As String
PDFPath = "C:\Path\To\Your\File.pdf" ' Set the desired PDF file path
With ActiveSheet.PageSetup
.PrintArea = "$A$1:$H$" & Simple_LastRowOnPage
End With
' Export the active sheet as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Make sure to change C:\Path\To\Your\File.pdf to the desired path where you want to save the PDF.
For printing the entire workbook as a PDF:
vba code:
Sub PRINT_WORKBOOK_PDF()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim PDFPath As String
PDFPath = "C:\Path\To\Your\File.pdf" ' Set the desired PDF file path
' Set the entire workbook as the print area
ActiveWorkbook.Sheets.PrintOut
' Export the entire workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
This code will export the active sheet or the entire workbook as a PDF file. You can change the PDFPath variable to specify the location and name of the PDF file you want to create.
Remember to replace "C:\Path\To\Your\File.pdf" with the desired path and filename for your PDF. You can also use a file dialog to let users choose the location and name for the PDF if needed.
Note that the file path should be a valid directory path on your computer, and the filename should end with ".pdf" for it to be saved as a PDF file. The text, _Steps and Code was created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.
Can I variabilise the path and file name using &range(C1) and put in C1 the path and file name
Better can I set up 2 variables:
one in C1 for the path
and one in D1 for the file name
it would read &RANGE(C1) &RANGE(D1)
OR SOMETHING SIMILAR IF YOU KNOW BETTER