Minty
AWF VIP
- Local time
- Today, 11:19
- Joined
- Jul 26, 2013
- Messages
- 10,640
Hi All,
I have a file dialog I use to get the location of various files for importing (shown below).
I have an issue with a client who uses file redirection at a corporate level and keeps getting an error - Error 70 Permission Denied.
In my testing, if I set strInitialDir to anything that doesn't exist or a totally bobbins file location it simply reverts to the last place explorer was opened to. But not on their machines. My guess is that because the file path exists, but for some reason the file dialog can't get to it I get the error. Testing this is proving awkward as you can imagine.
I've tried capturing the error and resetting the initial dir to "" but that doesn't seem to work.
Anyone got any ideas?
I have a file dialog I use to get the location of various files for importing (shown below).
I have an issue with a client who uses file redirection at a corporate level and keeps getting an error - Error 70 Permission Denied.
In my testing, if I set strInitialDir to anything that doesn't exist or a totally bobbins file location it simply reverts to the last place explorer was opened to. But not on their machines. My guess is that because the file path exists, but for some reason the file dialog can't get to it I get the error. Testing this is proving awkward as you can imagine.
I've tried capturing the error and resetting the initial dir to "" but that doesn't seem to work.
Anyone got any ideas?
Code:
Public Function strGetFileFolderName(Optional strInitialDir As String = "", Optional lngType As Long = 4, Optional strPattern As String = "All Files,*.*") As String
'StrInitialDir = where the filedialog starts browsing
'lngType e.g. 'msoFileDialogFilePicker = 3, msoFileDialogFolderPicker =4, msoFileDialogOpen=1,msoFileDialogSaveAs=2
Dim fDialog As Object
Dim vFile As Variant, varEntry As Variant
strGetFileFolderName = ""
Set fDialog = Application.FileDialog(lngType)
With fDialog
.Title = "Browse for "
Select Case lngType
Case 1 'msoFileDialogOpen
.Title = .Title & "File to open"
Case 2 'msoFileDialogSaveAs
.Title = .Title & "File to SaveAs"
Case 3 'msoFileDialogFilePicker
.Title = .Title & "File"
Case 4 'msoFileDialogFolderPicker
.Title = .Title & "Folder"
End Select
Select Case strPattern
Case "Excel"
strPattern = "MS Excel,*.XLSX; *.XLSM; *.XLS"
Case "Access"
strPattern = "MS Access,*.ACCDB"
Case "PPT"
strPattern = "MS Powerpoint,*.PPTX; *.PPTM"
End Select
If lngType <> 4 Then
'Reset then add filter patterns separated by tildes (~) where
' multiple extensions are separated by semi-colons (;) and the
' description is separated from them by a comma (,).
' Example strPattern :
' "MS Access,*.ACCDB; *.MDB~MS Excel,*.XLSX; *.XLSM; *.XLS"
Call .Filters.Clear
For Each varEntry In Split(strPattern, "~")
Call .Filters.Add(Description:=Split(varEntry, ",")(0), _
Extensions:=Split(varEntry, ",")(1))
Next varEntry
End If
'Set some default settings
.InitialFileName = strInitialDir
.AllowMultiSelect = False
.InitialView = 2 'msoFileDialogViewDetails
'Only return a value from the FileDialog if not cancelled.
If .Show Then strGetFileFolderName = .SelectedItems(1)
End With
ExitHere:
Exit Function
HandleErrors:
MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"
Resume ExitHere
End Function