ACCESS文件对话框Application.FileDialog的属性

574次阅读
没有评论

共计 2578 个字符,预计需要花费 7 分钟才能阅读完成。

提醒:本文最后更新于 2019-05-12 20:04,文中所关联的信息可能已发生改变,请知悉!

语法

表达式.FileDialog(dialogType)

表达式 一个代表 Application 对象的变量。

参数

名称 必需 / 可选 数据类型 说明
dialogType    必需      MsoFileDialogType      一个 MsoFileDialogType 常量,该常量代表对话框的类型。

MsoFileDialogType 可为以下 MsoFileDialogType 常量之一。

msoFileDialogFilePicker  允许用户选择文件。

msoFileDialogFolderPicker  允许用户选择一个文件夹。

msoFileDialogOpen  允许用户打开文件。

msoFileDialogSaveAs  允许用户保存一个文件。

注解


在 Microsoft Access 中不支持 msoFileDialogOpenmsoFileDialogSaveAs常量。

 

分别举例如下:

1、msoFileDialogFilePicker

1)选择单个文件

代码:

Sub SelectFile()‘选择单一文件

    With Application.FileDialog(msoFileDialogFilePicker)

        .AllowMultiSelect = False‘单选择

        .Filters.Clear‘清除文件过滤器

        .Filters.Add“Excel Files”,“*.xls;*.xlw”.Filters.Add“All Files”,“*.*”‘设置两个文件过滤器

        If .Show = -1 Then‘FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。MsgBox“您选择的文件是:”& .SelectedItems(1), vbOKOnly + vbInformation,“提示”End If

    End With

End Sub

2)选择多个文件

代码:

Sub SelectFile()‘选择多个文件

    Dim l As Long

    With Application.FileDialog(msoFileDialogFilePicker)

        .AllowMultiSelect = True‘单选择

        .Filters.Clear‘清除文件过滤器

        .Filters.Add“Excel Files”,“*.xls;*.xlw”.Filters.Add“All Files”,“*.*”‘设置两个文件过滤器

        .Show‘FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。For l = 1 To .SelectedItems.Count

            MsgBox“您选择的文件是:”& .SelectedItems(l), vbOKOnly + vbInformation,“提示”Next

    End With

End Sub

2、msoFileDialogFolderPicker

代码:

Sub SelectFolder()‘选择单一文件

    With Application.FileDialog(msoFileDialogFolderPicker)

        If .Show = -1 Then‘FileDialog 对象的 Show 方法显示对话框,并且返回 -1(如果您按 OK)和 0(如果您按 Cancel)。MsgBox“您选择的文件夹是:”& .SelectedItems(1), vbOKOnly + vbInformation,“提示”End If

    End With

End Sub

文件夹仅能选择一个

3、msoFileDialogOpen

4、msoFileDialogSaveAs

使用方法与前两种相同

只是在.show

可以用.Execute 方法来实际打开或者保存文件。

FileDialog 常用属性

Title 标题

Filter

设置过滤字符

FilterIndex

指定列表框中的默认的选项

AllowMultiSelect

是否多选

 

示例


此示例阐释如何使用 FileFialog 对象显示一个允许用户选择一个或多个文件的对话框。然后,将所选的文件添加到一个名为 FileList(文件列表)的列表框中。

Private Sub cmdFileDialog_Click() 
  
   ' Requires reference to Microsoft Office 11.0 Object Library. 
 
   Dim fDialog As Office.FileDialog 
   Dim varFile As Variant 
 
   ' Clear listbox contents. 
   Me.FileList.RowSource = "" 
 
   ' Set up the File Dialog. 
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
 
   With fDialog 
 
      ' Allow user to make multiple selections in dialog box 
      .AllowMultiSelect = True 
             
      ' Set the title of the dialog box. 
      .Title = "Please select one or more files" 
 
      ' Clear out the current filters, and add our own. 
      .Filters.Clear 
      .Filters.Add "Access Databases", "*.MDB" 
      .Filters.Add "Access Projects", "*.ADP" 
      .Filters.Add "All Files", "*.*" 
 
      'Show the dialog box. If the .Show method returns True, the' user picked at least one file. If the .Show method returns 
      ' False, the user clicked Cancel. 
      If .Show = True Then 
 
         'Loop through each file selected and add it to our list box. 
         For Each varFile In .SelectedItems 
            Me.FileList.AddItem varFile 
         Next 
 
      Else 
         MsgBox "You clicked Cancel in the file dialog box." 
      End If 
   End With 
End Sub

摘自 http://www.accessoft.com
正文完
 0
水东柳
版权声明:本站原创文章,由 水东柳 2019-01-27发表,共计2578字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)