在VBA中将FileDialog的文件夹默认为 [英] Defaulting a folder for FileDialog in VBA

查看:135
本文介绍了在VBA中将FileDialog的文件夹默认为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub Command93_Click()

    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant
    Dim P As String
    Dim DeleteEverything As String

        DoCmd.SetWarnings False
        DeleteEverything = "DELETE * FROM [TABLE]"
        DoCmd.RunSQL DeleteEverything

    Set f = Application.FileDialog(3)
    f.AllowMultiSelect = False
    If f.Show Then
        For Each varItem In f.SelectedItems
            strFile = Dir(varItem)
            strFolder = Left(varItem, Len(varItem) - Len(strFile))
            P = strFolder & strFile
        Next
    End If
    Set f = Nothing

        DoCmd.TransferText acImportFixed, "[IMPORT SPECIFICATION]", "[TABLE]", P, False

End Sub

如何在最初打开FileDialog时将其默认设置为特定文件夹?

How to make the FileDialog default to a specific folder when it initially opens?

推荐答案

将文件夹路径(包括结尾的\)添加到InitialFileName.例如,要打开用户主目录中的对话框,请执行以下操作:

Add the folder path (including the trailing \) to InitialFileName. For example, to open the dialog in the user's home directory do:

f.InitialFileName = Environ("USERPROFILE") & "\"

如果忘记了结尾的\, 对话框仍然会在正确的文件夹中打开, 但是文件夹名称也将显示为默认的选定文件名. 然后,对话框将寻找同名的子文件夹,该子文件夹通常不存在.

If you forget the trailing \, then the dialog box will still open in the correct folder, but the folder name will also appear as the default selected file name. Then the dialog will be looking for a sub-folder with the same name, which usually doesn't exist.

这篇关于在VBA中将FileDialog的文件夹默认为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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