vba中的OpenFileDialog,以字符串形式返回目录
问题描述:
我到处都是,我很惊讶这在VBA中还不容易作为功能使用.
I have been looking everywhere and I am very surprised that this is not already easily available as a function in VBA.
我需要一个函数,当该函数被调用时,它会打开一个文件对话框,人们可以选择1个文件(不是更多,只有1个),然后该函数以字符串形式返回文件的位置(包括文件名和扩展名).
I need a function that when called, opens a filedialog where people can select 1 file (not more, just 1) and then the function returns the location of the file (including filename+extension) as a string.
起初,我想:这有多难,在VB.NET中我真的不是很简单."
At first i thought: " How hard can that be, I'ts really simple in VB.NET.."
提前谢谢!
答
您是说像htis吗?
Sub Sample()
Dim ofD As Object
Dim Fil
Set ofD = Application.FileDialog(3)
ofD.AllowMultiSelect = False
ofD.Show
For Each Fil In ofD.SelectedItems
MsgBox Fil
Next
End Sub
如果AllowMultiSelect
是True
如果只有一个文件,这里是另一个示例.
Here is another example if there is only one file.
Sub Sample()
Dim ofD As Object
Dim Fil
Set ofD = Application.FileDialog(3)
ofD.AllowMultiSelect = False
If ofD.Show = False Then
MsgBox "User Pressed Cancel"
Else
MsgBox ofD.SelectedItems(1)
End If
End Sub