|
我也做了一个,并不算很完善,凑合先发上来
按要求是做一个调用最方便的对话框,首先想到的是把常用的类型先定义起来,同时也能支持自定义模式- Public Enum DialogType '定义对话框类型
- Fileopen = 1
- FileSaveAs = 2
- FilePicker = 3
- FolderPicker = 4
- End Enum
- Public Enum FilterType '定义筛选类型
- All = 0 '所有文件
- MsAccess = 1
- MsWord = 2
- MsExcel = 3
- Txt = 4
- HTML = 5
- Photo = 6
- Cust = 7 '自定义类型
- End Enum
- Public Const MsAllType As String = "*.*"
- Public Const MsALLTitle As String = "所有文件"
- 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
- '须引用 Micosoft Office 11.0 Object Library
- '用途: 自定义对话框
- '参数: DType 定义对话框类型 必选: Fileopen 打开 对话框;FileSaveAs 另存为 对话框;FilePicker 文件选取器 对话框;FolderPicker 文件夹选取器 对话框
- ' txtFilter 定义几种常用类型 支持自定义类型 必选;
- ' txtFilterName 可选,当txtFilter类型为Cust自定义,则txtFilterName表示自定义的筛选条件和名称,筛选条件和筛选名称用","分隔:亦表示保存的文件名
- ' strPath 文件路径 可选
- ' StrTitle 自定义对话框名称,可选;缺省为系统自带的
- '用法: GetFileName Fileopen, MsAccess
- ' GetFileName FileSaveAs, MsExcel, "Tes.xls"
- ' GetFileName Fileopen, Cust, "*.doc;*.xls;*.mdb"
- ' GetFileName Fileopen, Cust, "*.doc;*.xls;*.mdb,My Dialog"
- ' GetFileName Fileopen, Cust, "*.doc;*.xls;*.mdb,My Dialog", , "自定义标题"
- ' GetFileName Fileopen, MsAccess, , , "自定义标题"
- ' GetFileName Fileopen, Photo, , "D:"
- Dim StrFilter As String
- Dim StrFilterTitle As String
- Dim fDialog As Office.FileDialog
- Select Case txtFilter
- Case All
- StrFilter = MsAllType
- StrFilterTitle = MsALLTitle
- Case MsAccess
- StrFilter = MsAccessType
- StrFilterTitle = MsAccessTitle
- Case MsWord
- StrFilter = MsWordType
- StrFilterTitle = MsWordTitle
- Case MsExcel
- StrFilter = MsExcelType
- StrFilterTitle = MsExcelTitle
- Case Txt
- StrFilter = TxtType
- StrFilterTitle = TxtTitle
- Case HTML
- StrFilter = HtmType
- StrFilterTitle = HtmTitle
- Case Photo
- StrFilter = PhotoType
- StrFilterTitle = PhotoTitle
- Case Cust
- Dim Mypos
- If IsMissing(txtFilterName) Then
- MsgBox "你选择了自定义类型,请在txtFilterName选项中填写筛选数据"
- Exit Function
- Else
- Mypos = InStr(1, txtFilterName, ",")
- If Mypos > 0 Then
- StrFilter = Mid(txtFilterName, 1, Mypos - 1)
- StrFilterTitle = Mid(txtFilterName, Mypos + 1, Len(txtFilterName) - Mypos)
- Else
- StrFilter = txtFilterName
- StrFilterTitle = "自定义类型"
- End If
- End If
- End Select
- Set fDialog = Application.FileDialog(DType)
- With fDialog
- .AllowMultiSelect = False
- If IsMissing(strPath) Then
- .InitialFileName = CurrentProject.Path
- Else
- .InitialFileName = strPath
- End If
- If Not IsMissing(StrTitle) Then
- .Title = StrTitle
- End If
- If DType <> FileSaveAs Then
- .Filters.Clear
- .Filters.Add StrFilterTitle, StrFilter
- Else
- .InitialFileName = txtFilterName
- End If
- .Show
- End With
- If fDialog.SelectedItems.Count > 0 Then
- GetFileName = fDialog.SelectedItems(1)
- Else
- GetFileName = ""
- End If
- Set fDialog = Nothing
- End Function
复制代码 |
|