一、选择文件
(一)、GetOpenFilename
适用于OFFICE2000及以上版本
expression.GetOpenFilename(FileFilter, FilterIndex, Title, ButtonText, MultiSelect)
1、单一文件
Sub getfile()
fileToOpen = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
If fileToOpen <> False Then
Else
End IfEnd Sub
2、多个文件
Sub getfiles()
Dim filetoopen As Variant
Dim i As Integerfiletoopen = Application.GetOpenFilename("Excel Files (*.xls), *.xls", Title:="选择文件", MultiSelect:=True)
If Not IsArray(filetoopen) Then
Else
For i = LBound(filetoopen) To UBound(filetoopen)
Next
End IfEnd Sub
(二)、Application.FileDialog(不适用于OFFICE2000)
Sub getfile()
Dim lngCount As Long
With Application.FileDialog(msoFileDialogFilePicker )
.AllowMultiSelect = True
.Show
For lngCount = 1 To .SelectedItems.Count
MsgBox .SelectedItems(lngCount)
Next lngCount
End With
End Sub
二、调用目标文件夹
(一)、Application.FileDialog
不适用于OFFICE2000
Sub getfolder()
OFFICE2000可用Shell来调用
Sub Sample2()
Dim Shell, myPath
Set Shell = CreateObject("Shell.Application")
Set myPath = Shell.BrowseForFolder(0, "请选择文件夹", 0, 0)
If Not myPath Is Nothing Then MsgBox myPath.self.Path
Set Shell = Nothing
Set myPath = Nothing
End Sub
注:BrowseForFolder 方法
显示对话框由用户选择目录,可根据需求设定允许用户建立目录
语法
strDir = Shell.BrowseForFolder([varinitDir], [varTitle], [varDescription], [varNew])
参数
strDir
返回用户选择的目录名称,取消操作时为空
varinitDir
初始化目录,显示对话框时将自动定位到给定的目录
varTitle
对话框标题
varDescription
对话框描述
varNew
是否允许用户创建目录
详细资料可以参考:http://wenku.baidu.com/view/dbc899254b35eefdc8d3331c.html
评论