如何设置FileDialog不允许双击

我想使用文件打开对话框来提取文件path(如果更容易打开文件)

是否可以设置对话框,以便在双击文件名时不会打开文件? 我想避免的是,如果用户双击一个文件名,但该文件已经打开,则会出现进一步的警报。

或者,或者,如果我设置了这个function,当用户单击对话框的OPENbutton或双击文件名时打开文件的只读版本 – 这是一个更简单的方法吗? 在这种情况下,我使用对话框的Execute方法吗?

 Private Function FindFilePath() As Boolean Dim selectedMultiFiles As Boolean Dim fd As FileDialog Dim objfl As Variant Set fd = Excel.Application.FileDialog(msoFileDialogOpen) Dim myTxt As String With fd .Filters.Add "Excel Files", "*.xlsx;*.xlsm", 1 .AllowMultiSelect = False .Title = "Choose the file with the target table" .InitialView = msoFileDialogViewDetails If .Show = -1 Then myTxt = .SelectedItems.Item(1) fFileName = myTxt FindFilePath = True Else myTxt = "Nothing was selected" FindFilePath = False End If On Error Resume Next End With txBoxFilePath.Text = myTxt End Function 

我不确定这会干扰你当前的项目,但是你知道

 Dim getPath As Variant getPath = Application.GetOpenFilename Debug.Print getPath 

getPath将从字面上存储用户select的任何文件的path。

它不会自动打开文件,除非你真的Set getPath = App..

您可以稍后在您的代码中打开文件,执行检查文件是否已经打开,或者像您刚刚提到的那样只读打开文件。