VBA对话的FileFilter部分文件名(VBA Dialogue FileFilter Part

2019-06-24 14:11发布

我有几个.txt文件的目录。 比方说,

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

使用VBA文件的对话,我怎么可以过滤只显示“*的test.txt”下拉匹配的文件(即最后两个)? 或者我只能使用*。 过滤器?

下面好像它应该工作,但不会:

Sub TestIt()
   Dim test As Variant 'silly vba for not having a return type..
   test = Application.GetOpenFilename(FileFilter:="test (*test.txt), *test.txt")
End Sub

编辑:万一澄清这一不明确:我要过滤的,而不是“.TXT”文件“ 的test.txt”所以我只能从选择器hello_test.txt和test.txt的选择。

Answer 1:

我知道你是担心把文本文件名框中,但这正是你需要做什么,似乎是您的情况的标准。 我得到了完全相同的问题就挂了。

这是我用什么:

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


Answer 2:

如何FileDialog的?

Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
    .AllowMultiSelect = True
    .InitialFileName = "Z:\docs\*t*.*x*"
    .Show
End With

http://msdn.microsoft.com/en-us/library/aa213120(v=office.11​​).aspx



Answer 3:

这是你想什么呢? 一个模块中粘贴并运行子OpenMyFile

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
    lStructSize       As Long
    hwndOwner         As Long
    hInstance         As Long
    lpstrFilter       As String
    lpstrCustomFilter As String
    nMaxCustFilter    As Long
    nFilterIndex      As Long
    lpstrFile         As String
    nMaxFile          As Long
    lpstrFileTitle    As String
    nMaxFileTitle     As Long
    lpstrInitialDir   As String
    lpstrTitle        As String
    flags             As Long
    nFileOffset       As Integer
    nFileExtension    As Integer
    lpstrDefExt       As String
    lCustData         As Long
    lpfnHook          As Long
    lpTemplateName    As String
End Type

Sub OpenMyFile()
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim strFilter As String

    OpenFile.lStructSize = Len(OpenFile)

    '~~> Define your filter here
    strFilter = "Text File (*test.txt)" & Chr(0) & "*test.txt" & Chr(0)

    With OpenFile
        .lpstrFilter = strFilter
        .nFilterIndex = 1
        .lpstrFile = String(257, 0)
        .nMaxFile = Len(.lpstrFile) - 1
        .lpstrFileTitle = .lpstrFile
        .nMaxFileTitle = .nMaxFile
        .lpstrInitialDir = "C:\Users\Siddharth Rout\Desktop\"
        .lpstrTitle = "My FileFilter Open"
        .flags = 0
    End With

    lReturn = GetOpenFileName(OpenFile)

    If lReturn = 0 Then
        '~~> User cancelled
        MsgBox "User cancelled"
    Else
        MsgBox "User selected" & ":=" & OpenFile.lpstrFile
        '
        '~~> Rest of your code
        '
    End If
End Sub


文章来源: VBA Dialogue FileFilter Partial File Name