Blog

How to Convert Excel to PDF Using VBA

Uncategorized

How to Convert Excel to PDF Using VBA

Print Selection To PDF

This one is my personal favorite. This macro will convert the cells you actively have selected into a PDF.

If you only have one cell selected, the VBA macro is smart enough to realize that you probably don’t want to convert just one cell so it asks you to select the range you want to convert:

Sub PrintSelectionToPDF()
'SUBROUTINE: PrintSelectionToPDF
'DEVELOPER: Ryan Wells
'DESCRIPTION: Print your currently selected range to a PDF
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
If Selection.Count = 1 Then
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Else
Set ThisRng = Selection
End If
'Prompt for save location
strfile = "Selection" & "_" _
& Format(Now(), "yyyymmdd_hhmmss") _
& ".pdf"
strfile = ThisWorkbook.Path & "\" & strfile
myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")
If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
End If
End Sub
  • Donec porta ultricies urna, faucibus magna dapibus.
  • Etiam varius tortor ut ligula facilisis varius in a leo.
  • Folutpat tempor tur duis mattis dapibus, felis amet.
  1. Donec porta ultricies urna, faucibus magna dapibus.
  2. Etiam varius tortor ut ligula facilisis varius in a leo.
  3. Folutpat tempor tur duis mattis dapibus, felis amet.

Donec porta ultricies urna, nec faucibus magna dapibus vel. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam varius tortor ut ligula facilisis varius in a leo.

Leave your thought here

Your email address will not be published. Required fields are marked *

Categories

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare