Browse Window to Spcific Folder

mtagliaferri

Registered User.
Local time
Today, 00:30
Joined
Jul 16, 2006
Messages
550
I have the following line which opens the Browse Window in the current project, this folder has various subfolders inside, how can I direct to the specific folder example Folder "Photo" which is contained in the folder "General" which is in the "Current Project Folder"
Code:
fDialog.InitialFileName = CurrentProject.Path & "\Pic"
 
Dialog.InitialFileName = CurrentProject.Path & "\General\Photo"

to make your function more generic, create a parameter that can accept the path.
 
What you mean with
to make your function more generic, create a parameter that can accept the path.
 
This is what I typically use to browse to a specific folder and pick a file if that is what you are after

Code:
 Function BrowseFile(Optional strFilter As String, Optional strFolder As String) As String
Dim fdg As FileDialog, vrtSelectedItem As Variant
 Set fdg = Application.FileDialog(msoFileDialogFilePicker)
If strFilter <> "" Then
    fdg.Filters.Clear
    Select Case strFilter
        Case "Pictures"
            fdg.Filters.Add "Pictures", "*.jpg; *.gif; *.bmp"
        Case "PDF"
            fdg.Filters.Add "PDF", "*.pdf"
        Case "Excel"
            fdg.Filters.Add "Excel", "*.xlsx; *.xls; *.xlsm"
        Case "All"
            fdg.Filters.Add "All Files", "*.*"
    End Select
End If
 With fdg
    .AllowMultiSelect = False
    .InitialFileName = ""
    If Not IsNull(strFolder) And strFolder <> "" Then
        .InitialFileName = strFolder
    End If
    .InitialView = msoFileDialogViewDetails
    If .Show = -1 Then
        For Each vrtSelectedItem In .SelectedItems
            BrowseFile = vrtSelectedItem
        Next vrtSelectedItem
    End If
End With
  
Set fdg = Nothing
 End Function
 
how do i explain it, im not good in explaining. on your browse to folder function:

public function yourBrowseFunction(ByVal Optional strRelativeFolder As String = "") As String

then somewhere in your code would look like:

Dialog.InitialFileName = Replace(CurrentProject.Path & "\" & strRelativeFolderPath, "\\", "\")
 
This is what I typically use to browse to a specific folder and pick a file if that is what you are after

Code:
 Function BrowseFile(Optional strFilter As String, Optional strFolder As String) As String
Dim fdg As FileDialog, vrtSelectedItem As Variant
 Set fdg = Application.FileDialog(msoFileDialogFilePicker)
If strFilter <> "" Then
    fdg.Filters.Clear
    Select Case strFilter
        Case "Pictures"
            fdg.Filters.Add "Pictures", "*.jpg; *.gif; *.bmp"
        Case "PDF"
            fdg.Filters.Add "PDF", "*.pdf"
        Case "Excel"
            fdg.Filters.Add "Excel", "*.xlsx; *.xls; *.xlsm"
        Case "All"
            fdg.Filters.Add "All Files", "*.*"
    End Select
End If
 With fdg
    .AllowMultiSelect = False
    .InitialFileName = ""
    If Not IsNull(strFolder) And strFolder <> "" Then
        .InitialFileName = strFolder
    End If
    .InitialView = msoFileDialogViewDetails
    If .Show = -1 Then
        For Each vrtSelectedItem In .SelectedItems
            BrowseFile = vrtSelectedItem
        Next vrtSelectedItem
    End If
End With
  
Set fdg = Nothing
 End Function

I am always open to new solutions however this generates an error as in pic attached
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    64.3 KB · Views: 106
I am always open to new solutions however this generates an error as in pic attached

Look at the line it is highlighting

You have started a sub function and you have no End Sub statement.

I don't think that sub line should be there?
 
Strange.....its there!
Code:
Private Sub CmdAddPhoto_Click()
Function BrowseFile(Optional strFilter As String, Optional strFolder As String) As String
Dim fdg As FileDialog, vrtSelectedItem As Variant
 Set fdg = Application.FileDialog(msoFileDialogFilePicker)
If strFilter <> "" Then
    fdg.Filters.Clear
    Select Case strFilter
        Case "Pictures"
            fdg.Filters.Add "Pictures", "*.jpg; *.gif; *.bmp"
        Case "PDF"
            fdg.Filters.Add "PDF", "*.pdf"
        Case "Excel"
            fdg.Filters.Add "Excel", "*.xlsx; *.xls; *.xlsm"
        Case "All"
            fdg.Filters.Add "All Files", "*.*"
    End Select
End If
 With fdg
    .AllowMultiSelect = False
    .InitialFileName = ""
    If Not IsNull(strFolder) And strFolder <> "" Then
        .InitialFileName = strFolder
    End If
    .InitialView = msoFileDialogViewDetails
    If .Show = -1 Then
        For Each vrtSelectedItem In .SelectedItems
            BrowseFile = vrtSelectedItem
        Next vrtSelectedItem
    End If
End With
  
Set fdg = Nothing
End Function
End Sub
 
You need to put that in a module. Once you have it in a module from your subs you can call the following:

Browsefile "Excel", "C:\Windows\Whatever"

So you pass it what type of file you want to look for and it's default location.

Works like a champ.
 
Strange.....its there!
Code:
Private Sub CmdAddPhoto_Click()
Function BrowseFile(Optional strFilter As String, Optional strFolder As String) As String
Dim fdg As FileDialog, vrtSelectedItem As Variant
 Set fdg = Application.FileDialog(msoFileDialogFilePicker)
If strFilter <> "" Then
    fdg.Filters.Clear
    Select Case strFilter
        Case "Pictures"
            fdg.Filters.Add "Pictures", "*.jpg; *.gif; *.bmp"
        Case "PDF"
            fdg.Filters.Add "PDF", "*.pdf"
        Case "Excel"
            fdg.Filters.Add "Excel", "*.xlsx; *.xls; *.xlsm"
        Case "All"
            fdg.Filters.Add "All Files", "*.*"
    End Select
End If
 With fdg
    .AllowMultiSelect = False
    .InitialFileName = ""
    If Not IsNull(strFolder) And strFolder <> "" Then
        .InitialFileName = strFolder
    End If
    .InitialView = msoFileDialogViewDetails
    If .Show = -1 Then
        For Each vrtSelectedItem In .SelectedItems
            BrowseFile = vrtSelectedItem
        Next vrtSelectedItem
    End If
End With
  
Set fdg = Nothing
End Function
End Sub

Now you have a function inside a sub. Compilers going to love that.
 

Users who are viewing this thread

Back
Top Bottom