Excel Vba Print To Pdf And Save Site

Dim folder As String folder = ThisWorkbook.Path & "\" filePath = folder & "MyReport.pdf" Prevent duplicate names by adding the current date/time:

Dim timeStamp As String timeStamp = Format(Now, "yyyymmdd_hhnnss") filePath = "C:\Reports\Report_" & timeStamp & ".pdf" Avoid creating empty PDFs: excel vba print to pdf and save

'Get values from cells invoiceNum = ws.Range("B5").Value customerName = ws.Range("B6").Value customerName = Replace(customerName, " ", "_") 'Remove spaces Dim folder As String folder = ThisWorkbook

'Export the range rng.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=filePath, _ Quality:=xlQualityStandard MsgBox "Range exported to PDF." End Sub Hardcoding filenames is useless for automation. Instead, pull data from cells (e.g., invoice number and date). export code

'Loop through each worksheet For Each ws In ThisWorkbook.Worksheets ws.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:=folderPath & ws.Name & ".pdf", _ Quality:=xlQualityStandard Next ws

'Create dynamic path filePath = "C:\Invoices\" & invoiceNum & "_" & customerName & ".pdf"

Application.DisplayAlerts = False ' ... export code ... Application.DisplayAlerts = True Instead of hardcoding C:\... , save the PDF in the same folder as your Excel file: