3

因此,我正在使用 FileDialog 选择一个文件夹,其中包含我要放入 PowerPoint 演示文稿中的图像,但我在获取正确的初始视图时遇到了问题。

这是我所拥有的

strFolder = InitDir
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    .InitialFileName = "C:\Users\Daniel\My Pictures\" 'using to test code

    If Right(strName, 1) <> "\" Then
        strFolder = strFolder & "\"
    End If
    If .Show <> -1 Then
        Exit Sub
    Else
        strFolder = .SelectedItems(1) & "\"
    End If
End With

如果我在“我的图片”之后排除“\”,我的用户文件夹“Daniel”中的所有文件夹都会出现在最初选择“我的图片”的初始视图中,但是当我在之后包含“\”时,我假设是“FileDialogViewList”刚刚出现。我如何让“我的图片”文件夹最初出现。

4

4 回答 4

2

您传递的 initialFileName 不正确,这就是为什么它将“我的文档”显示为初始文件夹的原因。因为如果 initialFileName 不正确,则会显示默认目录。尝试以下路径:C:\Documents and Settings\Daniel\My Documents\My Pictures\

于 2013-06-16T10:39:24.357 回答
0

我遇到了类似的问题,在调试过程中注意到,如果文件或文件夹路径与在此对象初始化时为文件对话框对象设置的路径相同,则初始文件名未设置为你想要的只是一个用户文档文件夹。为了克服这个问题,我帮助了这个代码片段:

If Not fDialog.InitialFileName = filePath Then
    fDialog.InitialFileName = filePath
End If
于 2017-10-18T05:42:04.610 回答
0

我注意到预设的文件路径以某种方式被选中,这意味着文本是蓝色的......对我来说,这是解决方案:

' delete the selected text entry! 
SendKeys ("{Delete}") 

完整代码(对我有用):

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a folder"
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    If Not .InitialFileName = sFolder Then
        .InitialFileName = sFolder
    End If

    ' Apperently you need Sendkeys to delete the text entry...
    SendKeys ("{Delete}")

    If .Show = -1 Then ' OK was pressed
        sFolder = .SelectedItems(1) & "\"
    Else
        Exit Sub 'Cancel presssed, user wants to Quit.
    End If
End With
于 2022-02-26T02:12:59.060 回答
0

我现在看到,我忘记检查存储在 sFolder 中的预设路径是否以“\”结尾。我完全忽略了这一点,这解释了“奇怪”的行为。

如果您将此行放在 FileDialog 上方,那么一切正常,并且不需要丑陋的发送键“解决方案”... :-)

If Right(sFolder, 1) <> Application.PathSeparator Then sFolder = sFolder & Application.PathSeparators

完整代码(对我有用):

If Right(sFolder, 1) <> Application.PathSeparator Then sFolder = sFolder & Application.PathSeparators

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a folder"
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = msoFileDialogViewList
    If Not .InitialFileName = sFolder Then
        .InitialFileName = sFolder
    End If
    If .Show = -1 Then ' OK was pressed
        sFolder = .SelectedItems(1) & "\"
    Else
        Exit Sub 'Cancel presssed, user wants to Quit.
    End If
End With
于 2022-02-26T02:24:30.157 回答