Application.FileDialog(msoFileDialogFilePicker) with dynamic InitialFileName

Bering

Board Regular
Joined
Aug 22, 2018
Messages
186
Office Version
  1. 2016
Platform
  1. Windows
Hello guys,

I would like to modify this function so that the InitialFileName will change according to the user selected path except for the penultimate portion of the path.

For example, within a loop, if the first time the selected path is:

"U:\Mypath\MySubFolder\ABC\CDE\Filex.pdf"

I would like the function to "memorize"

.InitialFileName as "U:\Mypath\MySubFolder\ABC\"

until the end of the loop.

No luck so far..


VBA Code:
Function GetFolderName(Optional OpenAt As String) As String
    Dim wbFullName As Variant

    With Excel.Application.FileDialog(msoFileDialogFolderPicker)
    wbFullName = Excel.Application.FileDialog(msoFileDialogFilePicker)

        .InitialFileName = "U:\Mypath\MySubFolder\"
        
        .Show
        
        GetFolderName = .SelectedItems(1)

    End With
    
    
End Function

Many thanks in advance for any suggestions!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Perhaps a module level variable where your function is located, or a public (global) variable somewhere else. No idea where based on what you've shown. Perhaps research variable scope to get an idea of what would be best. BTW, if user cancels, your code will error. You should wrap .Show in an If block, as in
If .Show Then
 
Upvote 0
Perhaps a module level variable where your function is located, or a public (global) variable somewhere else. No idea where based on what you've shown. Perhaps research variable scope to get an idea of what would be best. BTW, if user cancels, your code will error. You should wrap .Show in an If block, as in
If .Show Then
Thanks I will amend that!
I tried declaring GetFolderName as a public variable between option explicit and the Sub that calls this function, however I get an error message… maybe I should use a different name?
 
Upvote 0
You cannot use the same name for 2 objects like that. One object is the procedure, the other is the variable.
 
Upvote 0
Your question and code is a bit ambiguous. Your post shows you want to select a file and remember the parent's parent folder of the file, but your code has a function named GetFolderName which calls both FileDialog(msoFileDialogFolderPicker) and FileDialog(msoFileDialogFilePicker).

Assuming you want to use FileDialog(msoFileDialogFilePicker) to select a file and remember the folder two levels up, we can append the DOS/Windows "..\" parent folder syntax to the folder path, therefore append "..\..\" to go up 2 folder levels.

VBA Code:
Public Sub Test_Loop()

    Dim i As Long
    Dim initialFolder As String
    Dim selectedFile As String
    
    initialFolder = ""
    For i = 1 To 4
        MsgBox "initialFolder = " & initialFolder, Title:="Loop " & i
        selectedFile = GetFileName(initialFolder)
        If selectedFile <> "" Then
            initialFolder = Left(selectedFile, InStrRev(selectedFile, "\")) & "..\..\"
            MsgBox "selectedFile = " & selectedFile & vbCrLf & vbCrLf & _
                   "next initialFolder = " & initialFolder, Title:="Loop " & i
        End If
    Next
    
End Sub


Function GetFileName(Optional OpenAt As String) As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = OpenAt
        If .Show Then
            GetFileName = .SelectedItems(1)
        Else
            GetFileName = ""
        End If
    End With
    
End Function
 
Upvote 0
Solution
Your question and code is a bit ambiguous. Your post shows you want to select a file and remember the parent's parent folder of the file, but your code has a function named GetFolderName which calls both FileDialog(msoFileDialogFolderPicker) and FileDialog(msoFileDialogFilePicker).

Assuming you want to use FileDialog(msoFileDialogFilePicker) to select a file and remember the folder two levels up, we can append the DOS/Windows "..\" parent folder syntax to the folder path, therefore append "..\..\" to go up 2 folder levels.

VBA Code:
Public Sub Test_Loop()

    Dim i As Long
    Dim initialFolder As String
    Dim selectedFile As String
  
    initialFolder = ""
    For i = 1 To 4
        MsgBox "initialFolder = " & initialFolder, Title:="Loop " & i
        selectedFile = GetFileName(initialFolder)
        If selectedFile <> "" Then
            initialFolder = Left(selectedFile, InStrRev(selectedFile, "\")) & "..\..\"
            MsgBox "selectedFile = " & selectedFile & vbCrLf & vbCrLf & _
                   "next initialFolder = " & initialFolder, Title:="Loop " & i
        End If
    Next
  
End Sub


Function GetFileName(Optional OpenAt As String) As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = OpenAt
        If .Show Then
            GetFileName = .SelectedItems(1)
        Else
            GetFileName = ""
        End If
    End With
  
End Function
[/CODE
[/QUOTE]

Thank you so much!!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

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