+ Reply to Thread
Results 1 to 10 of 10

FileDialog Properties

Hybrid View

jlt199 FileDialog Properties 05-07-2010, 05:58 PM
JBeaucaire Re: FileDialog Properties 05-07-2010, 06:30 PM
jlt199 Re: FileDialog Properties 05-07-2010, 06:33 PM
JBeaucaire Re: FileDialog Properties 05-07-2010, 06:41 PM
jlt199 Re: FileDialog Properties 05-07-2010, 06:49 PM
JBeaucaire Re: FileDialog Properties 05-07-2010, 06:41 PM
broro183 Re: FileDialog Properties 05-08-2010, 12:33 PM
JBeaucaire Re: FileDialog Properties 06-16-2010, 07:20 PM
jlt199 Re: FileDialog Properties 06-16-2010, 07:41 PM
JBeaucaire Re: FileDialog Properties 06-17-2010, 02:53 AM
  1. #1
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    FileDialog Properties

    Hi again,

    I have another question!

    I have written this function to open a dialog box to allow the user to select the text file to read

    Function getFile() As String
        Dim fd As FileDialog
        Dim ffs As FileDialogFilters
        Dim fname As String
        
        
        Set fd = Application.FileDialog(msoFileDialogOpen)
        
        With fd
            Set ffs = .Filters
            
            With ffs
                .Clear
                .Add "Text Files", "*.txt"
            End With
            
            .AllowMultiSelect = False
            
            If .Show = False Then Exit Function
            
            getFile = .SelectedItems(1)
        End With
    End Function
    which works fine. My question is, how can I update this function to also allow files without extensions to be shown?

    The researcher who will be applying my macro usually forgets to save the data in a .txt file and instead leaves them without extensions.

    I'm currently running the function without the line
     .Add "Text Files", "*.txt"
    which shows all files, but if the folder in question contains a lot of other files this seems needlessly messy.

    Many thanks
    Last edited by jlt199; 05-07-2010 at 06:40 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: FileDialog Properties

    Change the filter from "*.txt" to "*.*"
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    Re: FileDialog Properties

    Thanks for your reply, but that will give me all files in the folder.

    I'm looking to only see files with a .txt extension or without any extension at all. Meaning that all others, such as .doc or .xls are not shown.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: FileDialog Properties

    Quote Originally Posted by jlt199 View Post
    I'm looking to only see files with a .txt extension or without any extension at all
    I really don't think that creature exists. The option above is the best fall back I can think of.

  5. #5
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    Re: FileDialog Properties

    That's a shame, thanks for your help

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: FileDialog Properties

    Actually, if you do this, you can keep the txt default, and give yourself an "All Files" option in the File Type drop window:
            With ffs
                .Clear
                .Add "Text Files", "*.txt"
                .Add "All Files", "*.*"
            End With

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: FileDialog Properties

    hi,

    I think the same as JB - that there probably is no creature like this. An alternative may (?) be to loop through each file & only Add it to the list if there is no "." in the filename string. Or create a list box which opens instead of the standard dialog box that is also populated by looping. I'm just guessing as I haven't tested either of these approaches.

    Here's another guess... Could you eradicate the problem by asking your researcher to change their settings?
    Ask them to open Windows Explorer - Tools - Options - View - Files & Folders, untick "hide extensions for known file types", then press [Apply to All Folders] & [ok].

    I've been told this is good practice for the unrelated reason that some viruses etc duplicate file names & icons but can often be identified if the extension is shown b/c they will end in ".exe" (instead of the expected ".doc" or ".txt" etc).

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: FileDialog Properties

    Hey, back again. From a suggestion given by someone on another forum, try this:

    Sub test()
    
    Dim fPath As String
    ChDir "C:\2010"
    
        With Application.FileDialog(msoFileDialogOpen)
            .AllowMultiSelect = False
            .Filters.Clear
            .Filters.Add "All Files", "*.*"        'everything
            .Filters.Add "Text Files", "*.txt", 1  'default
            .InitialFileName = "*."
            .Show
            If .SelectedItems.Count > 0 Then
                fPath = .SelectedItems(1) & "\"
            Else
                Exit Sub
            End If
        End With
    End Sub

    I've underlined the idea suggested, it sets the initial display to only show files with no extension at all.

  9. #9
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    Re: FileDialog Properties

    Thanks for your reply. If I edit my function to

    Function getFile() As String
        ''Opens dialog box for user to select text file to read.
        Dim fname As String
    
        With Application.FileDialog(msoFileDialogOpen)
            With .Filters
                .Clear
                .Add "Text Files", "*.txt", 1
                .Add "All Files", "*.*", 2
            End With
            
            .InitialFileName = "*."
            .AllowMultiSelect = False
            
            If .Show = False Then Exit Function
            
            getFile = .SelectedItems(1)
        End With
    End Function
    Then is does indeed initially only show the files without an extension, but it then won't let me use the other filters to find the .txt files, or all files. Which ever option I select from the dropdown I can only see the files without an extension.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: FileDialog Properties

    I can't speak to your version, but my version will show all the text files, as long as I press the DELETE key first to remove that default entry of "*." then the filters work again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1