vba中的OpenFileDialog,以字符串形式返回目录 [英] OpenFileDialog in vba that returns the directory as a string

查看:188
本文介绍了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

如果AllowMultiSelectTrue

如果只有一个文件,这里是另一个示例.

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

这篇关于vba中的OpenFileDialog,以字符串形式返回目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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