In this example we will print the whole workbook and save the file name as the name on our code. STEPS:
VBA Code:
Sub Print_Workbook() Dim loc As String loc = "E:\Workbook.pdf" ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _ filename:=loc End Sub
VBA Code Explanation
Sub Print_Workbook()
Sub is part of the code that will not return any value. It is also known as subprocedure. So we name our procedure Print_Workbook().
loc = "E:\Workbook.pdf"
This line is to select the location and the pdf file name. Here, we save our file in E: on our computer and name the file Workbook.
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, filename:=loc
This line of code is for exporting the excel file as PDF and making it ready for print.
End Sub
This will end the procedure.
STEPS:
VBA Code:
Sub Print_Sheet() Dim loc As String loc = "E:\Worksheet.pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ filename:=loc End Sub
STEPS:
VBA Code:
Sub PrntPDF() ActiveWindow.SelectedSheets.PrintOut Copies:=1, _ Collate:=True, ActivePrinter:="Adobe PDF" Dim fnam As String fnam = Range("B4").Value End Sub
STEPS:
VBA Code:
Sub PrntPDF1() Dim wrksht As Worksheet Dim sht As Variant Set sht = ActiveWindow.SelectedSheets For Each wrksht In sht wrksht.Select wrksht.ExportAsFixedFormat Type:=xlTypePDF, _ filename:=ThisWorkbook.Path & "/" & wrksht.Name & ".pdf" Next wrksht sht.Select End Sub
This will save the file as the sheet number of the workbook.
VBA Code Explanation
For Each wrksht In sht wrksht.Select wrksht.ExportAsFixedFormat Type:=xlTypePDF, filename:=ThisWorkbook.Path & "/" & wrksht.Name & ".pdf" Next wrksht
This line of code is for exporting the excel file as a pdf and printing the file.
STEPS:
VBA Code:
Sub PrntPDF2() Dim loc As String loc = "E:\Sheet6.pdf" ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ filename:=loc, _ OpenAfterPublish:=False, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ Quality:=xlQualityStandard, _ From:=1, To:=2 End Sub
This will also save the pdf file as the sheet number.
VBA Code Explanation
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=loc, OpenAfterPublish:=False, IncludeDocProperties:=True, IgnorePrintAreas:=False, Quality:=xlQualityStandard, From:=1, To:=2
This code block is for printing and saving the excel file as a pdf.
STEPS:
VBA Code:
Sub PrntPDF3() Dim wrks As Worksheet Dim wrkb As Workbook Dim snam As String Dim sloc As String Dim sf As String Dim slocf As String Dim myFile As Variant Dim l As Long On Error GoTo errHandler Set wrkb = ActiveWorkbook Set wrks = ActiveSheet sloc = wrkb.Path If sloc = "" Then sloc = Application.DefaultFilePath End If sloc = sloc & "\" snam = wrks.Range("A1").Value & " Print " & wrks.Range("A2").Value & " PDF " & wrks.Range("A3").Value sf = snam & ".pdf" slocf = sloc & sf If PrintFile(slocf) Then l = MsgBox(vbQuestion + vbYesNo, "File Exists") If l <> vbYes Then myFile = Application.GetSaveAsFilename(InitialFileName:=slocf, FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Save File Name") If myFile <> "False" Then slocf = myFile Else GoTo exitHandler End If End If End If wrks.ExportAsFixedFormat Type:=xlTypePDF, filename:=slocf, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False MsgBox "Print PDF: " & vbCrLf & strPathFile exitHandler: Exit Sub errHandler: MsgBox "Not Print" Resume exitHandler End Sub Function PrintFile(rsFullPath As String) As Boolean PrintFile = CBool(Len(Dir$(rsFullPath)) > 0) End Function
STEPS:
VBA Code:
Sub PrintPDF4() Dim wrksht As Worksheet Dim wrkbk As Workbook Dim snam As String Dim sloc As String Dim sf As String Dim slocf As String Dim file As Variant On Error GoTo errHandler Set wrkbk = ActiveWorkbook Set wrksht = ActiveSheet sloc = wrkbk.Path If sloc = "" Then sloc = Application.DefaultFilePath End If sloc = sloc & "\" snam = wrksht.Range("A1").Value & " - " & wrksht.Range("A2").Value & " - " & wrksht.Range("A3").Value sf = snam & ".pdf" slocf = sloc & sf wrksht.ExportAsFixedFormat Type:=xlTypePDF, filename:=slocf, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False MsgBox "Print PDF: " & vbCrLf & strPathFile exitHandler: Exit Sub errHandler: MsgBox "Not Print" Resume exitHandler End Sub
VBA Code Explanation
sloc = wrkbk.Path If sloc = "" Then sloc = Application.DefaultFilePath End If sloc = sloc & "\" snam = wrksht.Range("A1").Value & " - " & wrksht.Range("A2").Value & " - " & wrksht.Range("A3").Value
This pulls up the active workbook folder where the workbook is saved.
sf = snam & ".pdf" slocf = sloc & sf
This will create the default name for saving files.
wrksht.ExportAsFixedFormat Type:=xlTypePDF, filename:=slocf, Quality:=xlQualityStandard, _ IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
That block exports the excel file to PDF in the current folder.
MsgBox "Print PDF: " & vbCrLf & strPathFile exitHandler: Exit Sub errHandler: MsgBox "Not Print" Resume exitHandler
This will create a confirmation message with file information.
STEPS:
VBA Code:
Sub PrintPDF5() Dim loc As String Dim r As Range loc = "E:\PDF File.pdf" Set rng = Sheets("IT").Range("A1:F13") rng.ExportAsFixedFormat Type:=xlTypePDF, _ filename:=loc End Sub
STEPS:
VBA Code:
Sub Prnt_PDF() Call Automatic_Name End Sub Function Automatic_Name() As Boolean ' Copies sheets into new PDF file for e-mailing Dim sht As String, file As String, loc As String Dim s As String Application.ScreenUpdating = False sht = ActiveSheet.Name file = ActiveWorkbook.Name loc = ActiveWorkbook.Path s = loc & "\" & sht & ".pdf" On Error Resume Next ActiveSheet.PageSetup.PrintQuality = 600 err.Clear On Error GoTo 0 On Error GoTo RefLibError ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=s, Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True On Error GoTo 0 SaveOnly: MsgBox "Saved as .pdf file: " & vbCrLf & vbCrLf & SvAs & _ "Review the .pdf document." Automatic_Name = True GoTo EndMacro RefLibError: MsgBox "Unable to save as PDF." Automatioc_Name = False EndMacro: End Function
Download Practice Workbook
You can download the workbook and practice with them.