【VBA】Get file name

You can get “file name“

PR

VBA Code

As an example, get the file name of the file “sample.txt”

sample.txt
Option Explicit

Sub sample()
    
    Dim fso As Object
    Dim filePath As String
    Dim fileName As String
    
    'Set "File path"
    filePath = "C:\Users\user\Desktop\sample.txt"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    'Get file name
    fileName = fso.GetFile(filePath).Name
    
    MsgBox ("File Name : " & fileName)
    
    Set fso = Nothing
    
End Sub

Set “File path”(Line 10).

Get file name(Line 15).

PR

Result

You got file name!

Result
Result
PR

FYI

For more information about following, see the following official online manual:

GetFile method” of “FileSystemObject”


Name property” of “File Object”

タイトルとURLをコピーしました