【问题标题】:possible to have characters preceding * in FileDialog filter?FileDialog 过滤器中可能有 * 前面的字符?
【发布时间】:2015-08-04 18:23:08
【问题描述】:

我想创建一个 FileDialog,它的过滤器只允许匹配表达式 xyz*.xlsm 的文件;这将允许选择像 xyz123.xlsmxyzzat.xlsm 这样的文件,但不能选择 xyz123.docxabc123.xlsm

我正在使用此代码:

Sub testfd()

Dim fd As FileDialog

Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Filters.Add "xyz*", "xyz*.xlsm", 1
If fd.Show = -1 Then
    Debug.Print fd.SelectedItems(1)
Else
    Debug.Print "xyz"
End If


End Sub

但是,fd.Filters.Add 行会生成此运行时错误:

Invalid procedure call or argument

使用过滤器*.xlsm正常工作。

难道不能像我在上面的代码中那样使用.Filters.Add 吗?如果是这样,我如何确保用户只选择以给定字符序列开头和结尾的文件?

【问题讨论】:

  • 编辑:msdn.microsoft.com/en-us/library/office/… 使用类似 .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1 的东西。也许通配符不在两个子字符串之间?我不确定。
  • 我认为您不能使用这样的通配符:该参数仅用于指定扩展。
  • 如果您急需它,为什么不从头开始创建它呢?这有点耗时但很简单。我记得做过一次……
  • @SiddharthRout,您的意思是从头开始创建我自己的 FileDialog GUI 作为用户窗体?这似乎是一个相当复杂的项目。
  • 是吗?查看我发布的答案;)

标签: vba excel


【解决方案1】:

如果您急需它,为什么不从头开始创建它呢?这有点耗时但很简单。我记得做过一次...... – Siddharth Rout 39 分钟前

这是我为您创建的一个简单示例(花了大约 40 分钟创建它)。

如下图所示创建一个用户表单,然后按所示命名。

用户表单代码

将此代码粘贴到用户表单中

Option Explicit

Dim justStarted As Boolean

Private Sub UserForm_Initialize()
    With ListBox1
        .ColumnCount = 2
        .ColumnWidths = "70;60"
        .ListStyle = fmListStylePlain
    End With
    justStarted = True
End Sub

Private Sub UserForm_Activate()
    justStarted = False
    Populate
End Sub

'~~> Manually changing folder
Private Sub InitialPath_Change()
    If InitialPath = "" Or justStarted = True Then Exit Sub
    
    If Dir(InitialPath) <> "" Then
        Populate
    Else
        ListBox1.Clear
        TextBox2.Text = ""
    End If
End Sub

'~~> Listbox Single Click - File Selection
Private Sub ListBox1_Click()
    If ListBox1.ListIndex < 0 Then Exit Sub
    
    If ListBox1.List(ListBox1.ListIndex, 1) = "File" Then _
    TextBox2.Text = ListBox1.List(ListBox1.ListIndex)
End Sub

'~~> Listbox Double Click - Folder Open
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    If ListBox1.ListIndex < 0 Then Exit Sub
    
    If ListBox1.List(ListBox1.ListIndex, 1) = "Folder" Then
        If Right(Me.InitialPath, 1) <> "\" Then
            InitialPath = Me.InitialPath & "\" & ListBox1.List(ListBox1.ListIndex, 0) & "\"
        Else
            InitialPath = Me.InitialPath & ListBox1.List(ListBox1.ListIndex, 0) & "\"
        End If
        
        Populate
    End If
End Sub

'~~> Open Button
Private Sub CommandButton1_Click()
    If Len(Trim(TextBox2.Text)) = 0 Then Exit Sub
    
    If Right(Me.InitialPath, 1) <> "\" Then InitialPath = Me.InitialPath & "\"
    
    If Dir(InitialPath & TextBox2.Text) <> "" Then
        MsgBox "You selected " & InitialPath & TextBox2.Text
    Else
        MsgBox "Please select a valid file"
    End If
End Sub

'~~> Exit Button
Private Sub CommandButton2_Click()
    Unload Me
End Sub

'~~> Populate Listbox
Sub Populate()
    Dim sFile As Variant, sFolder As Variant
    Dim sFilter As String
    Dim pos As Long: pos = 0
    
    ListBox1.Clear
       
    Dim objFSO As Object, objFolder As Object, objSubFolder As Object
    Dim i As Integer
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(InitialPath)
    
    For Each objSubFolder In objFolder.subfolders
        With ListBox1
            .AddItem
            .List(pos, 0) = objSubFolder.Name
            .List(pos, 1) = "Folder"
            pos = pos + 1
        End With
    Next objSubFolder
    
    sFilter = Split(Filter, "(")(1)
    sFilter = Split(sFilter, ")")(0)
    Filter = sFilter
    
    sFile = Dir(InitialPath & Trim(sFilter))
    
    While (sFile <> "")
        With ListBox1
            .AddItem
            .List(pos, 0) = sFile
            .List(pos, 1) = "File"
            pos = pos + 1
        End With
        sFile = Dir
    Wend
End Sub

模块

你可以从一个模块中调用它

Sub Sample()
    With MyFileBrowser
        .InitialPath = "C:\Users\Siddharth\Desktop\"
        .Filter = "My Files,(*ture*.*)"
        .Caption = "Open"
        .Show
    End With
End Sub

行动中

免责声明

  1. 错误处理未完成。
  2. 仅适用于单个过滤器
  3. Filter 文本框已锁定,无法编辑

【讨论】:

  • 令人印象深刻!我会看看 PM 是否愿意这样做,或者他们是否需要 FileDialog 的全部功能(例如,新文件夹创建、多列排序、文件重命名等)
【解决方案2】:

您可能不再需要解决方案,但这可能对像我这样正在寻找此问题解决方案的其他用户有所帮助。

有一种方法可以过滤文件类型过滤器之外的文件。在以下示例中,我只想查看文件夹中以“AccountNr”中包含的字符串开头的 html 文件。 为此,我将 AccountNr 字符串和一个星号添加到 InitialFileNameInitialFileName 中的此添加不会更改文件夹的路径。 FileDialog 窗口中的选择器字段将显示带有星号的 AccountNr 字符串。 (基本代码在stackoverflow中找到,我根据自己的需要进行了修改)

Private Function GetURL(AccountNr)
    Dim fd As Office.FileDialog

    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    With fd

        .InitialFileName = "C:\Users\xxxed\Downloads\YY\" & AccountNr & "*"
                'set directory (initial file path) & AccountNr

        .AllowMultiSelect = False

        ' Set the title of the dialog box.
        .Title = "Please select the file."

        ' Clear out the current filters, and add our own.
        .Filters.Clear
        .Filters.Add "HTML Files", "*.html" 'This filters files with "AccountNr*.html"
        .Filters.Add "All Files", "*.*"     'alternative filter: all types

        ' Show the dialog box. If the .Show method returns True, the
        ' user picked at least one file. If the .Show method returns
        ' False, the user clicked Cancel.
        If .Show = True Then
            GetURL = .SelectedItems(1)      'This is the file name and path
        Else
            GetURL = ""
        End If
    End With
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2017-04-04
    • 2018-01-02
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多