Summary
Pulls up a browser dialog where a user can choose a file. The file name and full path to it are returned. The example code shown below results in the following dialog.
Syntax
GetFilePath[$]([DefName$], [DefExt$], [DefDir$], [Title$], [Option])
Description
Put up a dialog box and get a file path from the user. The returned string is a complete path and file name. If the cancel button is pressed then a null string is returned.
DefName$ Set the initial File Name in the to this string value. If this is omitted then *.DefExt$ is used.
DefExt$ Initially show files whose extension matches this string value. (Multiple extensions can be specified by using ";" as the separator.) If this is omitted then * is used.
DefDir$ This string value is the initial directory. If this is omitted then the current directory is used.
Title$ This string value is the title of the dialog. If this is omitted then ''Open" is used.
Option This numeric value determines the file selection options. If this is omitted then zero is used. See table below.
0 Only allow the user to select a file that exists.
1 Confirm creation when the user selects a file that does not exist.
2 Allow the user to select any file whether it exists or not.
3 Confirm overwrite when the user selects a file that exists.
+4 Selecting a different directory changes the application's current directory.
Example
Dim fullName As String fullName = GetFilePath(,"txt",Project.Path,"Select Text File")
Advanced Examples
The following code shows the simplest way I know of to get the file name and the path to the file from the GetFilePath() output.
Sub main Dim fullName As String Dim fileName As String Dim filePath As String Dim temp() As String Dim n As Integer Dim i As Integer fullName = GetFilePath(,"txt",Project.Path,"Select Text File") temp = Split(fullName,"\") n = UBound(temp) fileName = temp(n) filePath = "" For i=0 To n-1 filePath = filePath + temp(i) + "\" Next i Debug.Print fileName Debug.Print filePath End Sub
Another way of doing this
' Code Module Sub Main fn$ = GetFilePath$(,"xml",,"Choose XML File") 'gets full path and filename If fn$="" Then Exit Sub 'Cancel was pressed, exit the sub fname$ = Mid$(fn$,InStrRev(fn$,"\")+1) 'gets file name with extension fname_root$ = Left(fname$,InStr(1,fname$,".")-1) 'gets filename without extension tks = Split(fn$,fname$) fpath$ = tks(0) 'gets path to filename End Sub