gpt4 book ai didi

vba - 文件选择器,如何指定文件类型?

转载 作者:行者123 更新时间:2023-12-02 07:19:28 27 4
gpt4 key购买 nike

我有以下代码,用于打开文件选择器并让用户选择一个文件。

Sub SelectFiles(ByRef test As String)

Dim iFileSelect As FileDialog
Set iFileSelect = Application.FileDialog(msoFileDialogFilePicker)

Dim vrtSelectedItem As Variant

If iFileSelect.Show = -1 Then

For Each vrtSelectedItem In iFileSelect.SelectedItems
test = vrtSelectedItem
Next vrtSelectedItem

End If

Set iFileSelect = Nothing

End Sub

我希望它只显示某种类型的文件(在本例中为 XML),就像现在一样,用户可以选择任何文件类型。

我已经进行了一些创造性的 Google 搜索,但似乎找不到解决方案,而且我感觉它应该非常简单。

最佳答案

Sub Main()
Dim test As String
SelectFiles test
Debug.Print test
End Sub

Sub SelectFiles(ByRef test As String)

Dim iFileSelect As FileDialog
Set iFileSelect = Application.FileDialog(msoFileDialogOpen)
With iFileSelect
.AllowMultiSelect = True
.Title = "Select XML Files"
.Filters.Clear
.Filters.Add "Extensible Markup Language Files", "*.xml"
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then
Dim vrtSelectedItem
For Each vrtSelectedItem In iFileSelect.SelectedItems
test = vrtSelectedItem
Next vrtSelectedItem
End If
End With
Set iFileSelect = Nothing
End Sub

关于vba - 文件选择器,如何指定文件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com