3

So I am using the FileDialog to select a folder full of images that I am putting into a PowerPoint presentation and I am having problems getting the right initial view to come up.

Here is what I have

strFolder = InitDir
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    .InitialFileName = "C:\Users\Daniel\My Pictures\" 'using to test code

    If Right(strName, 1) <> "\" Then
        strFolder = strFolder & "\"
    End If
    If .Show <> -1 Then
        Exit Sub
    Else
        strFolder = .SelectedItems(1) & "\"
    End If
End With

If I exlude the "\" after "My Pictures", all the folders in my user folder "Daniel" comes up in the initial view with "My Pictures" initially selected, but when I include the "\" after, what I presume is the "FileDialogViewList" just shows up. How do I get the "My Pictures" folder to come up initially.

Daniel Diaz
  • 33
  • 1
  • 1
  • 6

4 Answers4

3

The initialFileName that you have passed is incorrect that is why it is showing My Documents as an initial folder.Since in case of incorrect initialFileName default directory is shown. Try following path: C:\Documents and Settings\Daniel\My Documents\My Pictures\

Transformer
  • 379
  • 1
  • 8
  • That path didn't exist for me, it was C:\Documents and Settings\Daniel\My Pictures\ but it ended up doing the exact same as I wrote above. It's like it will only allow you to set the initialfilename at two folders into the drive if that makes sense. Like C:\1\2\ but not C:\1\2\3 or more. Is this just a limitation of using FileDialog or is there something I am missing still. – Daniel Diaz Jun 16 '13 at 21:44
  • you are right..However, i'll not say it a limitation...if a folder doesn't exist then how can it be assigned as initial file name.It will work for "C:\1\2\3\" if a directeory named 3 exists at this path. – Transformer Jun 18 '13 at 03:52
  • I figured it out after considering what you said. When I checked the properties of one of the folders in "My Pictures", its filepath used "Pictures" instead of "My Pictures" like was shown in the folder view, and that solved it. Thank you. – Daniel Diaz Jun 18 '13 at 04:44
0

I had the similar problem and during debugging noticed, that in the case when the file or folder path is the same as is set for the file dialog box object in the moment of this object initialization, initial file name ise set not to the one which you want but to a user Documents folder. To overcome this problem for me helped this code snippet:

If Not fDialog.InitialFileName = filePath Then
    fDialog.InitialFileName = filePath
End If
Sharunas Bielskis
  • 1,033
  • 1
  • 16
  • 25
0

I noticed that the preset filePath somehow got selected, meaning the text was BLUE... For me, this was the solution:

' delete the selected text entry! 
SendKeys ("{Delete}") 

Full code (which worked for me):

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a folder"
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    If Not .InitialFileName = sFolder Then
        .InitialFileName = sFolder
    End If

    ' Apperently you need Sendkeys to delete the text entry...
    SendKeys ("{Delete}")

    If .Show = -1 Then ' OK was pressed
        sFolder = .SelectedItems(1) & "\"
    Else
        Exit Sub 'Cancel presssed, user wants to Quit.
    End If
End With
Kees Podt
  • 1
  • 2
  • I now see, that I forgot to check if the prsesetpath stored in sFolder ended with a "\". If you place this line above the FileDialog, then all works fine and ther is no need for the ugly sendkeys "Solution"... :-) If Right(sFolder, 1) <> Application.PathSeparator Then sFolder = sFolder & Application.PathSeparators – Kees Podt Feb 26 '22 at 02:21
0

I now see, that I forgot to check if the presetpath stored in sFolder, ended with a "\". I totaly overlooked this, which explained the 'strange' behavior.

If you place this line above the FileDialog, then all works out fine and there is no need for the ugly sendkeys "Solution"... :-)

If Right(sFolder, 1) <> Application.PathSeparator Then sFolder = sFolder & Application.PathSeparators

Full code (which worked for me):

If Right(sFolder, 1) <> Application.PathSeparator Then sFolder = sFolder & Application.PathSeparators

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a folder"
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    If Not .InitialFileName = sFolder Then
        .InitialFileName = sFolder
    End If
    If .Show = -1 Then ' OK was pressed
        sFolder = .SelectedItems(1) & "\"
    Else
        Exit Sub 'Cancel presssed, user wants to Quit.
    End If
End With
Kees Podt
  • 1
  • 2