获取文件路径(以文件夹结尾)

时间:2011-05-11 22:22:01

标签: excel vba file-io

我知道如何让用户点击按钮导航到要打开的特定文件。

代码:

Private Sub CommandButton2_Click()
    Dim vaFiles As Variant

    vaFiles = Application.GetOpenFilename()

    ActiveSheet.Range("B9") = vaFiles
End Sub

我想要第二个按钮,让用户导航到一个文件夹,以保存我的程序创建的.pdf文件。

问题:GetOpenFilename要求用户点击文件。如果文件夹中没有文件,那么用户就无法做任何事情。

6 个答案:

答案 0 :(得分:23)

使用Application.FileDialog对象

Sub SelectFolder()
    Dim diaFolder As FileDialog
    Dim selected As Boolean

    ' Open the file dialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    selected = diaFolder.Show

    If selected Then
        MsgBox diaFolder.SelectedItems(1)
    End If

    Set diaFolder = Nothing
End Sub

答案 1 :(得分:5)

如果用户点击取消按钮而不是选择文件夹,则为此添加了ErrorHandler。因此,您不会收到可怕的错误消息,而是会收到一条消息,指出必须选择一个文件夹然后例程结束。下面的代码还将文件夹路径存储在范围名称中(它只链接到工作表上的单元格A1)。

Sub SelectFolder()

Dim diaFolder As FileDialog

'Open the file dialog
On Error GoTo ErrorHandler
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Title = "Select a folder then hit OK"
diaFolder.Show
Range("IC_Files_Path").Value = diaFolder.SelectedItems(1)
Set diaFolder = Nothing
Exit Sub

ErrorHandler:
Msg = "No folder selected, you must select a folder for program to run"
Style = vbError
Title = "Need to Select Folder"
Response = MsgBox(Msg, Style, Title)

End Sub

答案 2 :(得分:2)

在VBA编辑器的“工具”菜单中,单击“引用”...向下滚动到“Microsoft Shell控件和自动化”并选择它。

Sub FolderSelection()
    Dim MyPath As String
    MyPath = SelectFolder("Select Folder", "")
    If Len(MyPath) Then
        MsgBox MyPath
    Else
        MsgBox "Cancel was pressed"
    End If
End Sub

'Both arguements are optional. The first is the dialog caption and
'the second is is to specify the top-most visible folder in the
'hierarchy. The default is "My Computer."

Function SelectFolder(Optional Title As String, Optional TopFolder _
                         As String) As String
    Dim objShell As New Shell32.Shell
    Dim objFolder As Shell32.Folder

'If you use 16384 instead of 1 on the next line,
'files are also displayed
    Set objFolder = objShell.BrowseForFolder _
                            (0, Title, 1, TopFolder)
    If Not objFolder Is Nothing Then
        SelectFolder = objFolder.Items.Item.Path
    End If
End Function

Source Link

答案 3 :(得分:1)

使用Application.GetSaveAsFilename()的方式与使用Application.GetOpenFilename()

的方式相同

答案 4 :(得分:1)

如果要默认浏览到文件夹: 例如" D:\ Default_Folder" 只需初始化" InitialFileName"属性

Dim diaFolder As FileDialog

' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.InitialFileName = "D:\Default_Folder"
diaFolder.Show

答案 5 :(得分:0)

这可能会帮助你:

Sub SelectFolder()
    Dim diaFolder As FileDialog
    Dim Fname As String

    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show

    Fname = diaFolder.SelectedItems(1)

    ActiveSheet.Range("B9") = Fname

End Sub