Consulting

Results 1 to 7 of 7

Thread: Application.FileDialog clean up

  1. #1

    Application.FileDialog clean up

    Hi,

    The code below opens the file dialog and shows all files with a user defined filter. The directory chosen is then used to create a string. However, if no file is chosen and the file picker is cancelled, it returns with an error message. I would like this to not happen and just cancel the script for tidyness' sake. Is this possible?
    Thanks
    Sub FileOpenDialogBox()
    With Application.FileDialog(msoFileDialogFilePicker)
    .Filters.Clear
    .Filters.Add "Images", "*" & file_type
    .AllowMultiSelect = False
    .Show

    fullpath = .SelectedItems.Item(1)
    partialpath = Left(fullpath, Len(fullpath) - 7)

    End With

    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please, use code tags !

    Sub M_snb()
      With Application.FileDialog(3)
        .InitialFileName = "*.jpg"
        if .Show then
          fullpath = .SelectedItems(1)
          partialpath = replace(fullpath,dir(fullpath),"")
        end if
      End With
    End Sub

  3. #3
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Try:
    Sub FileOpenDialogBox()
        With Application.FileDialog(msoFileDialogFilePicker)
            .Filters.Clear
            .Filters.Add "Images", "*" '& file_type
            .AllowMultiSelect = False
            '.Show
            If .Show <> -1 Then Exit Sub
            fullpath = .SelectedItems.Item(1)
            partialpath = Left(fullpath, Len(fullpath) - 7)
        End With
    End Sub
    Semper in excretia sumus; solum profundum variat.

  4. #4
    Thanks for the replyt, that seemed to open the FileDialog twice and then returned an error further up in the code which seems very strange.

  5. #5
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Look at the code again... the .Show is commented out!
    Semper in excretia sumus; solum profundum variat.

  6. #6
    Oh yes, sorry for that. It works seamlessly now, Thanks Paul!

  7. #7
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •