11

ファイルダイアログで開くファイルのパス名とファイル名が必要です。この情報をワークシートにハイパーリンクで表示したいと考えています。

このコードでは、ファイル パスがあります。

Sub GetFilePath()

Set myFile = Application.FileDialog(msoFileDialogOpen)
With myFile
    .Title = "Choose File"
    .AllowMultiSelect = False
    If .Show <> -1 Then
        Exit Sub
    End If
FileSelected = .SelectedItems(1)
End With

ActiveSheet.Range("A1") = FileSelected
End Sub

ファイル名を取得する方法をまだ探しています。

4

12 に答える 12

13

FileSystemObject を使用して、ファイル パスの任意の部分を取得できます。GetFileName(filepath) は、必要なものを提供します。

以下の変更されたコード:

Sub GetFilePath()
Dim objFSO as New FileSystemObject

Set myFile = Application.FileDialog(msoFileDialogOpen)
With myFile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FileSelected = .SelectedItems(1)
End With

ActiveSheet.Range("A1") = FileSelected 'The file path
ActiveSheet.Range("A2") = objFSO.GetFileName(FileSelected) 'The file name
End Sub
于 2013-08-23T17:43:58.673 に答える
12

これを試して

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
于 2012-10-02T09:24:59.513 に答える
6

パスからファイル名のみを抽出するには、次のようにします。

varFileName = Mid(fDialog.SelectedItems(1), InStrRev(fDialog.SelectedItems(1), "\") + 1, Len(fDialog.SelectedItems(1)))
于 2015-10-02T12:22:07.910 に答える
3

私はあなたがこれを望んでいると思います:

Dim filename As String
filename = Application.GetOpenFilename

Dim cell As Range
cell = Application.Range("A1")
cell.Value = filename
于 2012-10-02T09:19:14.090 に答える
1

これがあなたが望むものに到達する最も簡単な方法だと思います。

最初の部分に対する JMK の回答へのクレジット。ハイパーリンク部分は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
于 2014-04-04T06:16:58.353 に答える
0

これでうまくいくと思います:

Dim filename As String
filename = Application.GetOpenFilename
于 2014-12-02T08:34:55.710 に答える
0

コードはルートコロンからファイル検索を開始します。特定のディレクトリから検索を開始したい場合は、毎回そのディレクトリに移動することを避けます。みたいにやった

Sub GetFilePath()
FileSelected = "G:\Audits\A2010"
Set myFile = Application.FileDialog(msoFileDialogOpen)
With myFile
.Title = "Choose File"
.AllowMultiSelect = False
If .Show <> -1 Then
Exit Sub
End If
FileSelected = .SelectedItems(1)
End With

ActiveSheet.Range("C14") = FileSelected
End Sub

しかし、「G:\Audits\A2010」からはリーチを開始できませんでした。

于 2014-03-08T09:10:09.857 に答える
-1

さまざまな Web サイトを検索して、ファイルを開くダイアログから完全な一体型情報が取得されたら、ファイル名から完全なパスを分離する方法についての解決策を探し、与えられた解決策が Excel の初心者にとっていかに「複雑」であるかを確認しました。私のように、もっと簡単な解決策があるのではないかと思いました。それで私は自分でそれに取り組み始め、この可能性にたどり着きました。(誰かが以前に同じ考えを持っていたかどうかはわかりません。非常に単純なので、もし誰かが持っていたら、失礼します。)

Dim fPath As String
Dim fName As String
Dim fdString As String

fdString = (the OpenFileDialog.FileName)

'Get just the path by finding the last "\" in the string from the end of it
 fPath = Left(fdString, InStrRev(fdString, "\"))

'Get just the file name by finding the last "\" in the string from the end of it
 fName = Mid(fdString, InStrRev(fdString, "\") + 1)

'Just to check the result
 Msgbox "File path: " & vbLF & fPath & vbLF & vblF & "File name: " & vbLF & fName

以上です!!!試してみて、どうなるか教えてください...

于 2017-03-08T04:23:44.880 に答える