Excel VBA:文件选择——Application.FileDialog

本文详细介绍了VBA中FileDialog对象的使用方法,包括如何创建和显示不同类型的文件对话框,如“打开”、“另存为”、“文件选取器”和“文件夹选取器”。通过示例代码展示了如何获取用户选择的文件或文件夹路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

FileDialog 对象

提供文件对话框,其功能与 Microsoft Office 应用程序中标准的“打开”“保存”对话框类似。

说明

使用 FileDialog 属性返回一个 FileDialog 对象。FileDialog 属性位于每个单独 Office 应用程序的 Application 对象中。该属性使用一个参数

DialogType确定该属性返回的 FileDialog 对象类型。FileDialog 对象有四种类型:

  • “打开”对话框 - 允许用户选择一个或多个可以在宿主应用程序中使用 Execute 方法打开的文件。
  • “另存为”对话框 - 允许用户选择一个文件,然后可以使用 Execute 方法将当前文件另存为该文件。
  • “文件选取器”对话框 - 允许用户选择一个或多个文件。用户选择的文件路径将捕获到 FileDialogSelectedItems 集合中。
  • “文件夹选取器”对话框 - 允许用户选择一个路径。用户选择的路径将捕获到 FileDialogSelectedItems 集合中。

每个宿主应用程序只能创建一个 FileDialog 对象实例。因此,即使创建多个 FileDialog 对象,FileDialog 对象的很多属性也会保持不变。所以,在显示对话框之前请确保已经针对用途适当地设置了所有属性。

示例

要使用 FileDialog 对象显示一个文件对话框,必须使用 Show 方法。显示了对话框后,在用户关闭对话框之前将不执行任何代码。下面的示例创建并显示一个“File Picker”对话框,并在消息框中显示每个选定的文件。

Sub Main()

    'Declare a variable as a FileDialog object.
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = Application.FileDialog(msoFileDialogFilePicker)

    'Declare a variable to contain the path
    'of each selected item. Even though the path is aString,
    'the variable must be a Variant because For Each...Next
    'routines only work with Variants and Objects.
    Dim vrtSelectedItem As Variant

    'Use a With...End With block to reference the FileDialog object.
    With fd

        'Use the Show method to display the File Picker dialog box and return the user's action.
        'The user pressed the button.
        If .Show = -1 Then

            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems

                'vrtSelectedItem is aString that contains the path of each selected item.
                'You can use any file I/O functions that you want to work with this path.
                'This example displays the path in a message box.
                MsgBox "The path is: " & vrtSelectedItem

            Next vrtSelectedItem
        'The user pressed Cancel.
        Else
        End If
    End With

    'Set the object variable to Nothing.
    Set fd = Nothing

End Sub

Application.FileDialog 属性

返回一个 FileDialog 对象,该对象表示文件对话框的实例。

语法

表达式.FileDialog(fileDialogType)

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

参数

名称必选/可选数据类型说明
fileDialogType必选MsoFileDialogType文件对话框的类型。

说明

MsoFileDialogType 可为以下 MsoFileDialogType 常量之一。
msoFileDialogFilePicker 允许用户选择一个文件。
msoFileDialogFolderPicker 允许用户选择一个文件夹。
msoFileDialogOpen 允许用户打开一个文件。
msoFileDialogSaveAs 允许用户保存一个文件。

示例

在本示例中,Microsoft Excel 打开文件对话框,允许用户选择一个或多个文件。选中这些文件之后,Excel 将逐条显示每个文件的路径。

Sub UseFileDialogOpen()

    Dim lngCount As Long

    ' Open the file dialog
    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .Show

        ' Display paths of each file selected
        For lngCount = 1 To .SelectedItems.Count
            MsgBox .SelectedItems(lngCount)
        Next lngCount

    End With

End Sub

source:VBA Help

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值