使用Application.FileDialog选择一个文件夹 [英] Picking a folder using Application.FileDialog

查看:224
本文介绍了使用Application.FileDialog选择一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Application.FileDialog来让用户选择一个文件夹,如下所示:

I'm using Application.FileDialog to let the user select a folder, as in:

Dim F As FileDialog
Set F = Application.FileDialog(msoFileDialogFolderPicker)

在这种情况下,默认文件夹不包含子文件夹,因此用户看到的是一个空框.理想情况下,该对话框不仅会列出文件夹,还会列出禁用/显示为灰色的文件,以便用户能够看到他选择的文件夹的内容.

In this case the default folder contains no subfolders, so what the user sees is an empty box. Ideally, the dialog would not just list folders, but would list files disabled/grayed out so that the user would be able to see the contents of the folder he is picking.

是否可以通过FileDialog廉价地实现此目的,还是必须创建自己的表单(ugh)?

Is there a way to do this on the cheap with a FileDialog or do I have to create my own form (ugh) ?

推荐答案

这是我数据库中的内容.我已经有一段时间在 VBA 中使用它了.此代码不是我的[strong> 我的代码,我很久以前就已经在网上找到了.

Here is something from my database. I have been using this for quite sometime now for VBA. This code is not mine and I found it long time ago on the web.

Sub Sample()
    ret = BrowseForFolder("C:\")
End Sub

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
    Dim ShellApp As Object

    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0

    Set ShellApp = Nothing

    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
        GoTo Invalid
    End Select

    Exit Function

Invalid:
    BrowseForFolder = False
End Function

这篇关于使用Application.FileDialog选择一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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