Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File Path (ends with folder)

Tags:

file-io

excel

vba

I know how to let the user click on a button to navigate to a specific file to open.

Code:

Private Sub CommandButton2_Click()
    Dim vaFiles As Variant

    vaFiles = Application.GetOpenFilename()

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

I want a second button that will let the user navigate to a folder to save the .pdf file that my program creates.

The problem: The GetOpenFilename requires the user to click on a file. If there's no file in the folder then there's nothing the user can do.

like image 828
Jesse Smothermon Avatar asked May 11 '11 22:05

Jesse Smothermon


People also ask

How do you get the full path of a file in a folder?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get the directory from a file's full path Python?

To get current file's full path, you can use the os. path. abspath function. If you want only the directory path, you can call os.

What is the last part of a file path called?

Extension or Filename Extension or File Extension: I like the last one.

What is absolute path extract directory?

Techopedia Explains Absolute PathAn absolute path always contains the root elements and the complete list of directories to locate the specific file or folder. All the information required to locate the file or folder is available in the absolute path.


2 Answers

Use the Application.FileDialog object

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
like image 59
chris neilsen Avatar answered Sep 21 '22 16:09

chris neilsen


In the VBA Editor's Tools menu, click References... scroll down to "Microsoft Shell Controls And Automation" and choose it.

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.

like image 39
ray Avatar answered Sep 22 '22 16:09

ray