Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Folder picker starting path 1

Status
Not open for further replies.

UnsolvedCoding

Technical User
Jul 20, 2011
424
US

It was requested that the folder picker start at a specific location on the F drive. I am not aware of how to do this using the folder or file picker, does anyone know how?

Here's the code.

Option Explicit

Sub Get_Path()

' This get path has the user select a folder and then
' simply puts the path in cell B1. No files are shown
' in the selected folder.

' Just in case something goes wrong
On Error GoTo Error_Handler

Application.StatusBar = "Obtaining path for files"

' msoFileDialogFilePicker is the same except for the name

Dim lngCount As Long
Dim sFileName

' Open the file dialog
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = True
.Show

' Display paths of each file selected
For lngCount = 1 To .SelectedItems.Count
sFileName = .SelectedItems(lngCount)
Next lngCount

End With

Range("B1").Value = sFileName

' Bye-bye
Exit Sub


Error_Handler:

'Tell the user what went wrong
Application.StatusBar = "Get_Path failed. " & Err.Description

' Set flag to indicate problem
Warning_Flag = 1

End Sub
 
I'd play with the InitialFileName property.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Try this:

Code:
Function BrowseForFolder(Optional OpenAt As Variant, Optional Prompt As String) As String
     'Function purpose:  To Browse for a user selected folder.
     'If the "OpenAt" path is provided, open the browser at that directory
     'If the "Promp" is provided it will appear below the dialog header bar.
     'NOTE:  If invalid, it will open at the Desktop level
     
    Dim ShellApp As Object
     
     'Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, Prompt, 0, OpenAt)
     
     'Set the folder to that selected.  (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0
     
     'Destroy the Shell Application
    Set ShellApp = Nothing
     
     'Check for invalid or non-entries and send to the Invalid error
     'handler if found
     'Valid selections can begin L: (where L is a letter) or
     '\\ (as in \\servername\sharename.  All others are invalid
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
        GoTo Invalid
    End Select
     
    Exit Function
     
Invalid:
     'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False
     
End Function

I'd credit whoever actually made it, but I don't remember where I got it from.
 
Using Inital File Name worked like a charm.

Made it read in the with statment and put a slash at the end of the path and it works great.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top