select PDF location through a FileDialog and remember this location

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet which contents need to be stored as a PDF file, in a certain folder.

Now I need to do two things:

1) select this folder through a FileDialog box
2) remember this folder for the future, however I still need to be able to change it's location if necessary.

Thanks in advance.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If you only need it to be remembered while Excel is opened (as in, on a 'per use' basis), you can declare the location as a shared string above the executed code.

VBA Code:
Dim SaveFolder As String

Sub GetAndSaveFolder()
    If SaveFolder = "" Then
        SaveFolder = GetFolder
    Else
        Dim rs As Variant
        rs = MsgBox("There is already a save folder selected. Would you like to pick a new save location?", vbYesNo)
        If rs = vbYes Then SaveFolder = GetFolder
    End If
End Sub

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function


If you want Excel to remember the location forever, or until it is overwritten, you will need to save that string somewhere on the computer. This can be one as a txt file or in the registry, though saving the file somewhere does require access to that location and can trigger virus scan protection.
 
Upvote 0
If you only need it to be remembered while Excel is opened (as in, on a 'per use' basis), you can declare the location as a shared string above the executed code.

VBA Code:
Dim SaveFolder As String

Sub GetAndSaveFolder()
    If SaveFolder = "" Then
        SaveFolder = GetFolder
    Else
        Dim rs As Variant
        rs = MsgBox("There is already a save folder selected. Would you like to pick a new save location?", vbYesNo)
        If rs = vbYes Then SaveFolder = GetFolder
    End If
End Sub

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function


If you want Excel to remember the location forever, or until it is overwritten, you will need to save that string somewhere on the computer. This can be one as a txt file or in the registry, though saving the file somewhere does require access to that location and can trigger virus scan protection.
Thanks Frabulator,

whereas your suggested solution is very valuable, meanwhile I've been working a bit as well, and I found a solution by storing the "SaveFolder" in a specific cell (G1) on the sheet.

This works fine but feel free to share your comments.

VBA Code:
Private Sub cmdPDF_Click()

Dim FldrPicker As FileDialog
Dim sPDFpath As String

On Error GoTo 0

 
sPDFpath = Range("G1").Value
 
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    .InitialFileName = sPDFpath
    If .Show <> -1 Then Exit Sub 'Check if user clicked cancel button
    sPDFpath = .SelectedItems(1) & "\"
    Range("G1").Value = sPDFpath
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,246
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top