Search code examples
excelvbapdfexportsave-as

Prompt user with "Save as" option when exporting Selection of cells as PDF


I want to prompt the user with the SaveAs dialog when exporting a specific Selection of cells as a PDF. I found the method .SaveAs but I struggle to understand how to use it for a specific range of cells.

Private Sub Selection_promptSaveAs_PDF()

  
Dim file_name As Variant
file_name = Application.GetSaveAsFilename(FileFilter:="Adobe PDF File_ (*.pdf), *.pdf")

If file_name <> False Then
ActiveWorkbook.SaveAs Filename:=file_name
MsgBox "File Saved!"
End If
End Sub

Solution

  • The Selection object has a method for that:

    Private Sub Selection_promptSaveAs_PDF()
        Dim file_name As Variant
    
        file_name = Application.GetSaveAsFilename(FileFilter:="Adobe PDF File_ (*.pdf), *.pdf")
    
        If file_name <> False Then
            Selection.ExportAsFixedFormat Type:=xlTypePDF, _
                FileName:=file_name, Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, IgnorePrintAreas:=False, _
                OpenAfterPublish:=False
            MsgBox "File Saved!"
        End If
    End Sub