如何用VBA打开选择文件和选择路径的对话框
2个回答
展开全部
'注意引用 microsoft office 10.0 (或以上) object library
'在文件对话框对中返回选择一个文件夹的路径.
Public Function ChooseFolder() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
With dlgOpen
If .Show = -1 Then
ChooseFolder = .SelectedItems(1)End IfEnd With
Set dlgOpen = Nothing
End Function
'--------------------------------------------------------
'在文件对话框对中,选择一个文件。
Public Function ChooseOneFile(Optional TitleStr As String
= 选择你要的文件, Optional TypesDec As String = 所有文件,
Optional Exten As String = *.*) As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
.Title = TitleStr
.Filters.Clear '清除所有的文件类型.
.Filters.Add TypesDec, Exten
.AllowMultiSelect = False '不能多选.
If .Show = -1 Then
' .AllowMultiSelect = True '多个文件
' For Each vrtSelectedItem In .SelectedItems
' MsgBox Path name: & vrtSelectedItem
' Next vrtSelectedItem
ChooseOneFile = .SelectedItems(1) '第一个文件End IfEnd With
'在文件对话框对中返回选择一个文件夹的路径.
Public Function ChooseFolder() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
With dlgOpen
If .Show = -1 Then
ChooseFolder = .SelectedItems(1)End IfEnd With
Set dlgOpen = Nothing
End Function
'--------------------------------------------------------
'在文件对话框对中,选择一个文件。
Public Function ChooseOneFile(Optional TitleStr As String
= 选择你要的文件, Optional TypesDec As String = 所有文件,
Optional Exten As String = *.*) As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
.Title = TitleStr
.Filters.Clear '清除所有的文件类型.
.Filters.Add TypesDec, Exten
.AllowMultiSelect = False '不能多选.
If .Show = -1 Then
' .AllowMultiSelect = True '多个文件
' For Each vrtSelectedItem In .SelectedItems
' MsgBox Path name: & vrtSelectedItem
' Next vrtSelectedItem
ChooseOneFile = .SelectedItems(1) '第一个文件End IfEnd With
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |