在Access运行的时候,打开一个文件,或者更改一个文件的路径,你可能会需要用到打开文件的对话框,之前很多人都使用API来做到,事实上还有更简单的办法。
以下代码需要引用“Microsoft Office 1x.0 Object Library”
Private Sub cmdAddFiles_Click()
'Declare a FileDialog object variable.
Dim myFileDialog As FileDialog
'Declare a variant to hold each file selected.
Dim vFileSelected As Variant
'Create a FileDialog object as an Open dialog window.
Set myFileDialog = Application.FileDialog(msoFileDialogOpen)
'If the user didn’t press Cancel, process each selection.
If myFileDialog.Show = –1 Then
For Each vFileSelected In myFileDialog.SelectedItems
lstFiles.AddItem vFileSelected
Next vFileSelected
Else
'The user pressed Cancel.
MsgBox “No files selected.”
End If
'Set the myFileDialog object variable to Nothing. Set myFileDialog = Nothing
End Sub
以上代码来源:《Microsoft® Access VBA Programming for the Absolute Beginner 》Second Edition
[此贴子已经被作者于2005-9-21 22:49:33编辑过]
|