Pass the 'FileDialogFilters' Collection and set to the FD.Filters Object

joeKra

Registered User.
Local time
Today, 12:55
Joined
Jan 24, 2012
Messages
208
Hi Guys
am trying to pass the FileDialogFilters collection to a method called "GetFileDialog" and am receiving an error that the object weren't SET... i tried to use the "NEW" keyword but doesn't seem to work for the FD object at-All. so my question is how can i initialize the collection object ?
Also, i noticed that the project will not compile this line "SET .Filters = sFilters" what am i doing wrong, i am just setting it to the same object type ?
THANKS IN ADVANCE !

Below is my code.

The btn Code:
Code:
Private Sub cmdAttachNew_Click()
    Dim sLoc As String, f As FileDialogFilters
    
    
    f.Add "Excel", "*.xlsx; *.xls", 1
    f.Add "All Files", "*.*", 2
    
    sLoc = GetFileDialog("%USERPROFILE%\Desktop", f)
                         

End Sub

GetFileDialog Code:
Code:
Function GetFileDialog(Optional sInitialFileName As String, Optional sFilters As FileDialogFilters) As String
    Dim fd As FileDialog, vrtSelectedItem As Variant
    Dim strSelectedFile As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
    .AllowMultiSelect = False
    .InitialView = msoFileDialogViewDetails
    .InitialFileName = sInitialFileName
[U]  SET   .Filters = sFilters[/U]
'    If sFilters > "" Then
'        arFilters = Split(sFilters, "|")
'        For i = 0 To UBound(arFilters) - 1
'            .Filters.Add "", arFilters(i), i + 1
'        Next
'    End If
    .Show
'    .Filters.Clear
    'If .Show = -1 Then
    'For Each vrtSelectedItem In .SelectedItems 'onby be 1
    'strSelectedFile = vrtSelectedItem
    'Next vrtSelectedItem
    'Else 'The user pressed Cancel.
    'End If
    End With
    If fd.SelectedItems.Count = 0 Then Exit Function
    GetFileDialog = fd.SelectedItems(1)
    Set fd = Nothing
 
Some classes you can't instantiate directly. Sometimes you can only get an instance if it is exposed by another class or method, and it appears that the FileDialogFilters collection may be like that. One work around is just use a normal collection, and pass in a delimited string that you can use to populate the FileDialogFilters collection inside your GetFileDialog routine.

So in your button click handler, do . . .
Code:
Private Sub cmdAttachNew_Click()
    Dim sLoc As String, 
    Dim f As New VBA.Collection [COLOR="Green"]'just use a VBA collection[/COLOR]
    
    f.Add "Excel|*.xlsx; *.xls" [COLOR="Green"]'delimit the filter data with a "|"[/COLOR]
    f.Add "All Files|*.*"
    
    sLoc = GetFileDialog("%USERPROFILE%\Desktop", f)
End Sub
And then in the other routine, wait till you have the FileDialog, and you can do . . .
Code:
Function GetFileDialog(Optional sInitialFileName As String, Optional cFilters As VBA.Collection) As String
[COLOR="Green"]   '...
   '...
[/COLOR]   With FileDialog(msoFileDialogFilePicker)[COLOR="Green"]    'once you have a fileDialog[/COLOR]
      if not cFilters is nothing then[COLOR="Green"]          'see if there are filters[/COLOR]
         dim i as integer
         dim vItem
         For i = 0 to cFilters.Count - 1       [COLOR="Green"]'enumerate the VBA collection[/COLOR]
            vItem = split(cFilters(i), "|")    [COLOR="Green"]'tease the data apart at the "|"[/COLOR]
            .Filters.add vItem(0), vItem(1), i [COLOR="Green"]'and add the filter to the FD Collection[/COLOR]
         next
[COLOR="Green"]         '...
[/COLOR]      end if
   end with
[COLOR="Green"]   '...
[/COLOR]end function
See what's going on there?
( None of this was tested so I'm sure the indexing is wrong somewhere, but this is the idea )
 

Users who are viewing this thread

Back
Top Bottom