PDA

View Full Version : Application.FileDialog clean up



willspill
12-19-2019, 01:55 AM
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

snb
12-19-2019, 01:58 AM
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

paulked
12-19-2019, 02:06 AM
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

willspill
12-19-2019, 03:23 AM
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.

paulked
12-19-2019, 03:38 AM
Look at the code again... the .Show is commented out!

willspill
12-19-2019, 03:59 AM
Oh yes, sorry for that. It works seamlessly now, Thanks Paul!

paulked
12-19-2019, 04:03 AM
:thumb