标题: 竞赛[高8]最好和最简洁的文件打开对话框功能 [打印本页] 作者: tmtony 时间: 2008-3-4 14:44 标题: 竞赛[高8]最好和最简洁的文件打开对话框功能 功能:使用代码调用文件打开对话框,只可使用Access或Office本身的功能,不可使用第三方Activex控件
要求:功能符合要求,代码最简洁,调用最方便
奖品:只取第一名 第一名增加金钱数80 增加经验值10 增加魅力10
期限:10天作者: 咱家是猫 时间: 2008-3-4 14:44
Function GetFileName(TitleText As String, Filter As String, FilterText As String, ByVal DialogType As Integer, Optional ByVal FilePath) As String
'参数DialogType说明:
'1.“打开”对话框
'2.“另存为”对话框(此项如执行类型筛选时会出错,所以加了出错处理为 Resume Next)
'3.“文件选取器”对话框
'4.“文件夹选取器”对话框
'例:AA = GetFileName("打开", "*.ini;*.txt", "配置文件", 1, 3)
On Error Resume Next
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(DialogType)
With dlgOpen
.Title = TitleText
.Filters.Clear
.Filters.Add FilterText, Filter
.AllowMultiSelect = False
If IsMissing(FilePath) Then
.InitialFileName = CurrentProject.Path
Else
.InitialFileName = FilePath
End If
.Show
End With
If dlgOpen.SelectedItems.Count > 0 Then
GetFileName = dlgOpen.SelectedItems(1)
Else
GetFileName = ""
End If
Set dlgOpen = Nothing
Set dlgOpen = Application.FileDialog( _
DialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
For i = 1 To dlgOpen.SelectedItems.Count
sStr = sStr & dlgOpen.SelectedItems(i) & vbCrLf
Next
MsgBox sStr
[ 本帖最后由 liwen 于 2008-3-4 16:25 编辑 ]作者: andymark 时间: 2008-3-4 16:16
这个当然行,但不是最好的,应该要写成模块,最起码能自定义后缀筛选作者: tmtony 时间: 2008-3-4 16:30
正是 至少可以指定扩展名作者: liwen 时间: 2008-3-4 16:33
Sub FilesSelect()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog( _
DialogType:=msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Filters.Add "AIS文件", "*.ais", 1
.Filters.Add "图象", "*.gif; *.jpg; *.jpeg; *.bmp", 1
.Show
End With
For i = 1 To dlgOpen.SelectedItems.Count
sStr = sStr & dlgOpen.SelectedItems(i) & vbCrLf
Next
MsgBox sStr
Public Const MsAccessType As String = "*.mdb;*.adp;*.mda;*.mde;*.ade"
Public Const MsAccessTitle As String = "Microsoft Office Access"
Public Const HtmType As String = "*.html;*.htm;*.hta;*.asp"
Public Const HtmTitle As String = "网页"
Public Const TxtType As String = "*.txt"
Public Const TxtTitle As String = "文本文档"
Public Const MsExcelType As String = "*.xl;*.xls;*.xla;*.xlm;*.xlc;*.xlw"
Public Const MsExcelTitle As String = "Microsoft Office Excel"
Public Const MsWordType As String = "*.doc;*.dot"
Public Const MsWordTitle As String = "Microsoft Office Word"
Public Const PhotoType As String = "*.JPG;*.JPEG;*.JPE;*.JFIF;*.GIF;*.bmp,*.PNG,*.ico"
Public Const PhotoTitle As String = "图片文件"
Public Function GetFileName(DType As DialogType, txtFilter As FilterType, Optional txtFilterName As String, Optional ByVal strPath, Optional StrTitle As String) As String