none
FileDialog Filters RRS feed

  • Question

  • Hello,

    I was wondering if it's possible, in a filedialog, to search for a part of the file instead of the extension. The function .Filter.Add I am using can't do this (can filter only if there is a dot at the begining, for example: ".docx").What I've been doing is inserting multiple dots in my filename so it could search for it, but it's not very esthetic. Does anyone have a trick for this?

    Here is a typical code where A1 is the cell where i write the part of the name i want to find. For example, if I write "h" I would like a filebox to open and showing up all the file with *h*  (hello.pdf, helicopter.pdf, Elephant.pdf, etc.).

    Thanks in advance!

    Sub Filepicker ()
    
    Dim fd as FileDialog
    Dim FileName as String
    
    'open filedialog application
    set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    'the filename is written in A1
    FileName = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    
    
    With fd
        .InitialFileName = "C:\ExampleFolder\" 'Startup folder
        .AllowMultiSelect = False 
        .Filters.Clear
        .Filters.Add "", "*" & FileName & "*.pdf" 'HERE
        .Show
    	If False Then
    		Exit Sub
    	End If
    
    End With
    
    
    
    'Rest of Code
    
    
    
    End Sub
    


    • Edited by Lopkest Thursday, July 21, 2016 5:23 PM
    Thursday, July 21, 2016 5:22 PM

Answers

  • Use the filter only to specify the extension:

        .Filters.Add "PDF Files", "*.pdf"

    And use the InitialFileName property to specify the pattern:

        .InitialFileName = "C:\ExampleFolder\*" & FileName & "*.pdf"


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Lopkest Thursday, July 21, 2016 8:12 PM
    Thursday, July 21, 2016 5:33 PM

All replies

  • Use the filter only to specify the extension:

        .Filters.Add "PDF Files", "*.pdf"

    And use the InitialFileName property to specify the pattern:

        .InitialFileName = "C:\ExampleFolder\*" & FileName & "*.pdf"


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Lopkest Thursday, July 21, 2016 8:12 PM
    Thursday, July 21, 2016 5:33 PM
  • Hello,

    Thanks for answering that quick. I am having another problem though. This is working about 1/4 the time im using it. Don't know why, but the rest of the time, the .pdf doesn't appear and the filedialog return nothing. I don't know if you could use of this information, but my :

    .InitialFileName = "C:\ExampleFolder\*" & FileName & "*.pdf"

    Looks more like this:

    .InitialFileName = C:\ExampleFolder\*" & FileName & Filter1 & Filter2 & Filter3 & ".pdf"

    Where Filter# are helping me filter the content in the file (I know that if Filter2 = B, then the content inside is a B Class). Example : Hello 3Ba.pdf

    Hope you can help,

    Thursday, July 21, 2016 6:24 PM
  • You could temporarily insert a line

        MsgBox "C:\ExampleFolder\*" & FileName & Filter1 & Filter2 & Filter3 & ".pdf"

    This will give you an idea what the dialog is looking for. Perhaps that will provide a clue.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, July 21, 2016 7:07 PM
  • Well, I ended up removing the .Filter.Add (when I said that the .pdf wasn't appearing) and now it's working. Not like I needed an extension filter since this is all pdf files. Thanks for your help!
    Thursday, July 21, 2016 8:12 PM