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

后端 未结 12 735
面向向阳花
面向向阳花 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: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
    

提交回复
热议问题