How to get selected path and name of the file opened with file dialog?

后端 未结 12 695
面向向阳花
面向向阳花 2020-12-29 12:14

I need the path name and file name of the file that is opened with File Dialog. I want to show this information with a hyperlink in my worksheet.

With this code I ha

相关标签:
12条回答
  • 2020-12-29 12:51

    From office 2010, we won't be able to use the common dialog box control, so it's nice to use the Application object to get the desired results.

    Here I got a text box and Command button - paste the following code under the command button click event, which will open the file dialog box and add the File name to the Text box.

    Dim sFileName  As String
    
    sFileName = Application.GetOpenFilename("MS Excel (*.xlsx), *.xls")
    
    TextBox1.Text = sFileName
    
    0 讨论(0)
  • 2020-12-29 12:52
    Sub GetFilePath()
    
    Set myFile = Application.FileDialog(msoFileDialogOpen)
    
    With myFile
    
    .Title = "Choose File"
    
    .AllowMultiSelect = False
    
    If .Show <> -1 Then
    
    Exit Sub
    
    End If
    
    FileSelected = Replace(.SelectedItems(1), .InitialFileName, "")
    
    End With
    
    ActiveSheet.Range("A1") = FileSelected
    
    End Sub
    
    0 讨论(0)
  • 2020-12-29 12:53

    I think this will do:

    Dim filename As String
    filename = Application.GetOpenFilename
    
    0 讨论(0)
  • 2020-12-29 12:54

    Try this

    Sub Demo()
        Dim lngCount As Long
        Dim cl As Range
    
        Set cl = ActiveCell
        ' Open the file dialog
        With Application.FileDialog(msoFileDialogOpen)
            .AllowMultiSelect = True
            .Show
            ' Display paths of each file selected
            For lngCount = 1 To .SelectedItems.Count
                ' Add Hyperlinks
                cl.Worksheet.Hyperlinks.Add _
                    Anchor:=cl, Address:=.SelectedItems(lngCount), _
                    TextToDisplay:=.SelectedItems(lngCount)
                ' Add file name
                'cl.Offset(0, 1) = _
                '    Mid(.SelectedItems(lngCount), InStrRev(.SelectedItems(lngCount), "\") + 1)
                ' Add file as formula
                cl.Offset(0, 1).FormulaR1C1 = _
                     "=TRIM(RIGHT(SUBSTITUTE(RC[-1],""\"",REPT("" "",99)),99))"
    
    
                Set cl = cl.Offset(1, 0)
            Next lngCount
        End With
    End Sub
    
    0 讨论(0)
  • 2020-12-29 12:58

    I think this is the simplest way to get to what you want.

    Credit to JMK's answer for the first part, and the hyperlink part was adapted from http://msdn.microsoft.com/en-us/library/office/ff822490(v=office.15).aspx

    'Gets the entire path to the file including the filename using the open file dialog
    Dim filename As String
    filename = Application.GetOpenFilename
    
    'Adds a hyperlink to cell b5 in the currently active sheet
    With ActiveSheet
     .Hyperlinks.Add Anchor:=.Range("b5"), _
     Address:=filename, _
     ScreenTip:="The screenTIP", _
     TextToDisplay:=filename
    End With
    
    0 讨论(0)
  • 2020-12-29 13:00

    I think you want this:

    Dim filename As String
    filename = Application.GetOpenFilename
    
    Dim cell As Range
    cell = Application.Range("A1")
    cell.Value = filename
    
    0 讨论(0)
提交回复
热议问题