0

In access I have implemented a routine that allows the user to choose an image file in a subfolder of the application folder. The routine was achieved as follows:

Public Function FotoOpen(DirStart As String, SubDir As String) As String
    ' Questa procedura apre il file dialog per la selezione della foto

    Dim PathDir As String
    PathDir = DirStart

   If SubDir <> "" Then
      PathDir = PathDir & "\" & SubDir & "\"
   End If

   '  Dim fDialog As Office.FileDialog
   Dim Fp As Office.FileDialog
   Set Fp = Application.FileDialog(msoFileDialogFilePicker)

   With Fp
      .Title = "Selezionare file immagine"
      .ButtonName = "Conferma"
      .InitialView = msoFileDialogViewPreview
      .Filters.Clear
      .Filters.Add "Tutti i Files (*.*)", "*.*"
      .Filters.Add "Images Files (*.jpg)", "*.jpg" '
      .Filters.Add "Images Files (*.bmp)", "*.BMP"
      .Filters.Add "Images Files (*.png)", "*.png"
      .Filters.Add "Images Files (*.ico)", "*.ico"
      .FilterIndex = 2
      .AllowMultiSelect = False
      .InitialView = msoFileDialogViewPreview
      .InitialFileName = PathDir

      If .Show = -1 Then
        FotoOpen = CStr(.SelectedItems.Item(1))
      Else
         Beep
       '  MsgBox "La selezione è stata annullata", vbInformation, "Informazione"
      End If

    End With
  Set Fp = Nothing
 End Function

But The Dialog doesn't seem to take into account the initial Dir passed, but it always opens on the Documents folder. Where's the mistake? Thank you all.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Antony
  • 83
  • 11
  • 1
    Your code works for me. Are you 100% sure you are passing correct path names to the function? Add `Debug.Print PathDir` and `Debug.Print Dir(PathDir)` after building `PathDir`. Perhaps you have double \\ in there. – Andre May 23 '20 at 08:00
  • 1
    Also make sure `PathDir` always ends with a '\' (or `Application.PathSeparator`), see: https://stackoverflow.com/questions/16917122/defaulting-a-folder-for-filedialog-in-vba – Luuk May 23 '20 at 10:02
  • @Andre Facendo il `Debug.Print PathDir` ottengo `H:\Sviluppo\SVILUPPO_VBA\TEMP4YOU\bmp\` che è corretto ma facendo il `Debug.Print .InitialFileName` ottengo `C:\Users\Antonio Lipone\OneDrive\Documenti\` che non è corretto. Sembra che l'istruzione: `.InitialFileName=PathDir` non va a buon fine. `InitialFileName` mantiene il suo valore originale, sebbene la documentazione dica altro. mi sembra un pò un mistero! Che ne pensi? – Antony May 26 '20 at 16:48
  • I don't speak Italian... but it seems you are missing the `\\` at the end of PathDir. – Andre May 26 '20 at 17:07
  • @Andre Solved. The problem was due because the SubDir had been changed from bmp to img so it was not found Thank you for everything – Antony May 27 '20 at 16:29

0 Answers0