在FileDialogfilter中可能有*之前的字符?

我想创build一个FileDialog,其filter只允许匹配expression式xyz*.xlsm ; 这将允许select像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 

使用filter*.xlsm正常工作。

是不是可以使用.Filters.Add我在上面的代码中.Filters.Add的方式? 如果是这样,我怎样才能确保用户只select文件的开始和结束给定的字符序列?

如果你非常需要它,为什么不从头开始创build呢? 这是稍微耗时,但很简单。 我记得曾经这么做过… – Siddharth Rout 39分钟前

这是我为你创build的一个简单的例子( 花费大约40分钟来创build它 )。

创build一个用户窗体,如下图所示,然后按照图示进行命名。

在这里输入图像说明

用户表单代码

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

 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. error handling未完成。
  2. 仅使用单个filter
  3. Filter文本框被locking以进行编辑

示例文件

https://www.dropbox.com/s/w6ckyp9xvgdshho/File%20Browser%20Example.xlsm?dl=0

可能你不再需要解决scheme了,但是这对于像我一样寻求解决这个问题的其他用户可能是有帮助的。

有一种方法可以过滤文件typesfilter之外的文件。 在下面的例子中,我只想看到文件夹中以“ AccountNr ”中包含的string开头的html文件。 为了实现这一点,我把AccountNrstring加星号添加到InitialFileName 。 该文件夹的path不会被InitialFileName中的添加更改FileDialog窗口中的select器字段将显示带有星号的AccountNrstring。 (基本代码在stackoverflow中find,我修改它为我的需要)

 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