Dialog 方法参考
📋 方法列表
| 方法 | 说明 |
|---|---|
ShowOpen | 显示打开文件对话框 |
ShowSave | 显示保存文件对话框 |
ShowBrowseForFolder | 显示选择文件夹对话框 |
SelectFiles | 选择文件并返回集合 |
SelectAndOpenFile | 选择文件并打开 |
OpenFile | 打开指定文件 |
AddFilter | 添加文件过滤器 |
ClearFilter | 清除所有过滤器 |
SetCommonFilters | 设置常用过滤器 |
SetImageFilters | 设置图片过滤器 |
SetDocumentFilters | 设置文档过滤器 |
SetCodeFilters | 设置代码过滤器 |
GetFilePath | 获取文件路径(不含文件名) |
GetFileName | 获取文件名(含扩展名) |
GetFileExtension | 获取文件扩展名 |
GetFileNameWithoutExt | 获取文件名(不含扩展名) |
Reset | 重置为默认设置 |
📂 ShowOpen 方法
说明
显示打开文件对话框。返回选中的文件路径,支持多选。
语法
vb
Public Function ShowOpen() As Variant返回值
- 单选模式:返回文件路径字符串,取消返回空字符串
- 多选模式(MultiSelect=True):返回文件路径数组
使用示例
单选文件
vb
Dim dlg As New cDialog
dlg.DialogTitle = "打开文件"
dlg.InitialDir = "C:\Documents"
dlg.AddFilter "文本文件", "*.txt"
dlg.AddFilter "所有文件", "*.*"
Dim filePath As String
filePath = dlg.ShowOpen
If filePath <> "" Then
Debug.Print "选择的文件: " & filePath
End If多选文件
vb
dlg.MultiSelect = True
Dim result As Variant
result = dlg.ShowOpen
If IsArray(result) Then
Dim i As Long
For i = LBound(result) To UBound(result)
Debug.Print "文件 " & i & ": " & result(i)
Next i
ElseIf result <> "" Then
Debug.Print "单个文件: " & result
End If💾 ShowSave 方法
说明
显示保存文件对话框。
语法
vb
Public Function ShowSave() As String返回值
返回保存路径字符串,取消返回空字符串。
使用示例
vb
Dim dlg As New cDialog
dlg.DialogTitle = "保存文件"
dlg.DefaultExt = "txt"
dlg.FileName = "新建文档.txt"
dlg.InitialDir = "C:\Documents"
dlg.AddFilter "文本文件", "*.txt"
dlg.AddFilter "所有文件", "*.*"
Dim savePath As String
savePath = dlg.ShowSave
If savePath <> "" Then
Debug.Print "保存到: " & savePath
' 执行保存操作
End If📁 ShowBrowseForFolder 方法
说明
显示选择文件夹对话框。
语法
vb
Public Function ShowBrowseForFolder() As String返回值
返回选择的文件夹路径,取消返回空字符串。
使用示例
vb
Dim dlg As New cDialog
dlg.DialogTitle = "请选择输出文件夹"
dlg.NewDialogStyle = True ' 使用新样式(带编辑框)
Dim folderPath As String
folderPath = dlg.ShowBrowseForFolder
If folderPath <> "" Then
Debug.Print "选择的文件夹: " & folderPath
End If📋 SelectFiles 方法
说明
显示打开文件对话框,返回选中的文件集合(Collection)。
语法
vb
Public Function SelectFiles() As Collection返回值
返回包含文件路径的 Collection 对象。
使用示例
vb
Dim dlg As New cDialog
dlg.DialogTitle = "选择文件"
dlg.MultiSelect = True
dlg.AddFilter "所有文件", "*.*"
Dim files As Collection
Set files = dlg.SelectFiles
Debug.Print "选择了 " & files.count & " 个文件"
Dim file As Variant
For Each file In files
Debug.Print file
Next file🚀 SelectAndOpenFile 方法
说明
显示打开文件对话框,并自动打开选中的文件。
语法
vb
Public Function SelectAndOpenFile() As Long返回值
返回 ShellExecute 的结果。
使用示例
vb
Dim dlg As New cDialog
dlg.DialogTitle = "选择要打开的文件"
dlg.AddFilter "可执行文件", "*.exe"
dlg.SelectAndOpenFile ' 选中后自动打开📂 OpenFile 方法
说明
使用 ShellExecute 打开指定文件。
语法
vb
Public Function OpenFile(ByVal strFilePath As String, Optional ByVal strOperation As String = "open") As Long参数
| 参数 | 类型 | 说明 |
|---|---|---|
strFilePath | String | 要打开的文件路径 |
strOperation | String | 操作类型,默认 "open" |
使用示例
vb
Dim dlg As New cDialog
dlg.OpenFile "C:\Documents\file.txt" ' 用默认程序打开
dlg.OpenFile "C:\Documents\file.txt", "edit" ' 用编辑方式打开➕ AddFilter 方法
说明
添加一个文件过滤器。
语法
vb
Public Sub AddFilter(ByVal strDescription As String, ByVal strExtension As String)参数
| 参数 | 类型 | 说明 |
|---|---|---|
strDescription | String | 过滤器描述 |
strExtension | String | 文件扩展名模式 |
使用示例
vb
dlg.AddFilter "文本文件", "*.txt"
dlg.AddFilter "图片文件", "*.jpg;*.png;*.gif"
dlg.AddFilter "所有文件", "*.*"🧹 ClearFilter 方法
说明
清除所有文件过滤器。
语法
vb
Public Sub ClearFilter()使用示例
vb
dlg.ClearFilter ' 清除之前的过滤器
dlg.AddFilter "新类型", "*.new"📋 预设过滤器方法
SetCommonFilters
设置常用文件过滤器(文本文件、所有文件)。
vb
dlg.SetCommonFiltersSetImageFilters
设置图片文件过滤器。
vb
dlg.SetImageFilters ' 包含 bmp, jpg, png, gif 等SetDocumentFilters
设置文档文件过滤器。
vb
dlg.SetDocumentFilters ' 包含 doc, xls, ppt, pdf 等SetCodeFilters
设置代码文件过滤器。
vb
dlg.SetCodeFilters ' 包含 bas, c, cs, java, py, js 等🔧 文件路径工具方法
GetFilePath
获取文件所在目录路径。
vb
Dim path As String
path = dlg.GetFilePath("C:\Folder\file.txt") ' 返回 "C:\Folder"GetFileName
获取文件名(含扩展名)。
vb
Dim name As String
name = dlg.GetFileName("C:\Folder\file.txt") ' 返回 "file.txt"GetFileExtension
获取文件扩展名(含点号)。
vb
Dim ext As String
ext = dlg.GetFileExtension("C:\Folder\file.txt") ' 返回 ".txt"GetFileNameWithoutExt
获取文件名(不含扩展名)。
vb
Dim name As String
name = dlg.GetFileNameWithoutExt("C:\Folder\file.txt") ' 返回 "file"🔄 Reset 方法
说明
重置所有设置为默认值。
语法
vb
Public Sub Reset()使用示例
vb
dlg.Reset ' 清空所有设置,恢复初始状态最后更新: 2026-05-17