Tuesday 28 April 2020

SAVE EXCEL IN PDF (ANY LOCATION) - MACRO / VBA


Namaste My Dear Friends!

I hope you all are doing well and Ready for another New Macro Thing.

In this blog we will write a macro for How to Save Excel file or selection in excel in PDF format at our desired location.

We'll cover this topic in 2 Steps: -

First, we will learn how to save the Folder Location in a cell in excel.




Second, how to save the excel in pdf at the location given in cell above.



Please refer the following Video for step by step tutorial: -








VBA Codes for: -

a) BROWSE

Sub Browse_Path()
Dim FolderS As FileDialog
Dim fPath As String
Set FolderS = Application.FileDialog(msoFileDialogFolderPicker)
With FolderS
    .Title = "Select a Folder" 'Title Set
    .AllowMultiSelect = False
    If .Show <> -1 Then GoTo Cancel 'Minimum 1 Folder should be Selected
       fPath = .SelectedItems(1)
    Range("G4").Value = fPath
Cancel:
    Set FolderS = Nothing
End With
End Sub


b) SAVE PDF

Sub Save_PDF()
    Dim flName As String, fPath As String, fPathfile As String
    Range("E1:N17").Select
    fPath = Range("G4").Value & "\"
    flName = "ABC.pdf"
    fPathfile = fPath & flName
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPathfile, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub






No comments:

Post a Comment