English 中文(简体)
VBA 对话文档名称
原标题:VBA Dialogue FileFilter Partial File Name

我有一份目录,有几卷txt文档。 说 明

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

借助于VBA的档案对话,我如何能够在下降时只展示“测试.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”而不是“.txt”文档,因此,我只能从选择人中挑选。

最佳回答

我看到,你对把案文放在档案箱中感到关切,但这正是你需要做的事,似乎是你情况的准则。 我只想谈谈同样的问题。

这是我使用的:

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
问题回答

如何指控?

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/a213120(v=office.11).aspx

这是你试图做的吗? 在一个模块中处理此事,并操作<代码>。 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:UsersSiddharth RoutDesktop"
        .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




相关问题
Handling no results for docmd.applyfilter

I have an Access app where I use search functionality. I have a TextBox and a Search Button on the form, and it does a wildcard search of whatever the user enters in the TextBox, and displays the ...

Outlook 2007 CommandBarControl.Execute won t work

I recently switched to Outlook 2007 and noticed that my VBA-macros won t work. I use the following code to open a new appointment-item (and fill it automatically). It worked perfect in Outlook 2003, ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

MS Access: list macro from VBA

I have to deal with a few macros (not VBA) in an inherited Access application. In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by ...

热门标签