VBA Dialogue FileFilter Partial File Name

前端 未结 3 1306
北荒
北荒 2021-01-19 04:53

I have a directory with several .txt files. Let\'s say

hi.txt
hello.txt
hello_test.txt
test.txt

Using a file dialogue in VBA, how can I fil

3条回答
  •  借酒劲吻你
    2021-01-19 05:29

    I see that you are concerned about putting text in the file name box, but that is exactly what you need to do and appears to be the norm for your situation. I got hung up on the exact same issue.

    This is what I used:

    Public Sub Browse_Click()
    
    Dim fileName As String
    Dim result As Integer
    Dim fs
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select Test File"
        .Filters.Add "Text File", "*.txt"
        .FilterIndex = 1
        .AllowMultiSelect = False
        .InitialFileName = "*test*.*"
    
        result = .Show
    
        If (result <> 0) Then
            fileName = Trim(.SelectedItems.Item(1))
    
            Me!txtFileLocation = fileName
    
        End If
    End With
    

提交回复
热议问题