Sub SaveActiveSheetAsPDF() Dim ws As Worksheet Dim desktopPath As String Dim fileName As String Dim fullPath As String ' Set the worksheet you want to print Set ws = ActiveSheet ' Get the user's Desktop path dynamically desktopPath = CreateObject( "WScript.Shell" ).SpecialFolders(
End Sub
)).Select ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fullPath Use code with caution. Copied to clipboard 3. Save in the Same Folder as the Excel File
Column A = Customer Name, Column B = Email, Columns C:G = Invoice Data. excel vba print to pdf and save
Sub ExportRangeToPDF() Dim rng As Range Dim filePath As String 'Define the range (e.g., A1:F20) Set rng = ThisWorkbook.Sheets("SalesData").Range("A1:F20") filePath = "C:\PDF Reports\SalesSummary.pdf"
Dim timeStamp As String timeStamp = Format(Now, "yyyymmdd_hhnnss") filePath = "C:\Reports\Report_" & timeStamp & ".pdf"
On Error GoTo 0
: Use xlQualityStandard for high quality or xlQualityMinimum for smaller file sizes.
Using Excel VBA to print to PDF and save files is not just a coding exercise—it is a practical skill that can save hours of manual work. Whether you are generating a single report or thousands of personalized PDFs, the ExportAsFixedFormat method gives you complete control.
'Export the range rng.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=filePath, _ Quality:=xlQualityStandard Sub ExportRangeToPDF() Dim rng As Range Dim filePath
End Sub
'Create folder if missing If Dir(FolderPath, vbDirectory) = "" Then MkDir FolderPath End If
: The exact folder destination and name you want to give the file. Quality:=xlQualityStandard : Ensures high quality for printing. You can change this to xlQualityMinimum to reduce the file size for emailing. IgnorePrintAreas:=False : When set to 'Export the range rng
Sub EnsureFolderAndSave() Dim FolderPath As String Dim FilePath As String FolderPath = ThisWorkbook.Path & "\MonthlyReports" FilePath = FolderPath & "\Report_" & Format(Date, "mm-yyyy") & ".pdf"