文件或文件夹选择器组合对话框 [英] File or Folder Picker combination dialog

查看:134
本文介绍了文件或文件夹选择器组合对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种易于部署的解决方案来解决我遇到的问题.我需要向用户显示一个对话框,允许他们自由浏览计算机并选择文件夹或文件.提交后,我需要将选定对象的路径传递回VBA宏.有人知道吗?

TIA,
Chad

I''m looking for an easily deployed solution to a problem I am having. I need to present the user with a dialog box that will allow them to browse their computer freely and select a folder or a file. Upon submit, I need the path of selected object passed back to a VBA macro. Any one have a clue?

TIA,
Chad

推荐答案

这很简单,我是从Excel 2003中获得的,据我所知它可能仍然可以工作,您可能需要测试更高的版本. br/>
This is pretty simple, I got this from Excel 2003, you may need to test for later versions, as far as I know it should still work.

Sub cmdOpenFileDialog()
    '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 a String,
    '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 action button.
        If .Show = -1 Then
            'Step through each string in the FileDialogSelectedItems collection.
            For Each vrtSelectedItem In .SelectedItems
                'vrtSelectedItem is a String 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 simply 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



问候

[更新]
上面的答案很好用,但是现在我在工作,我发现我一直在使用GetOpenFilename 方法打开我想从中导入数据的文件.



Regards

[Update]
The answer above works well but now that I am here at work I see I''ve been using GetOpenFilename method to open files that I wish to import data from.

fileToOpen = Application _
    .GetOpenFilename("Text Files (*.txt), *.txt")
If fileToOpen <> False Then
    MsgBox "Open " & fileToOpen
End If



这样更快,因为您可以在方法调用中指定过滤器,而上面的示例仅针对文本文件过滤.



This is quicker because you can specify filters in the method call, the above example filters for just text files.


这篇关于文件或文件夹选择器组合对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆