I want to get the name of the file with its extension after the parent file path. For example: retrieve picture.jpg from C:\Desktop\Pictures\picture.jpg. *Note: I do not know what the name of the file or picture is going to be. Users will add those themselves and I want to retrieve the name to display it on a form.
Here is what I have so far...
Private Sub InsertImageButton_Click()
Dim strImageID As String
Dim strFolder As String
Dim fso As Object
Dim strFormat As String
Const strParent = "C:\Desktop\Pictures\"
strImageID = Me.ImageID.Value
strFormat = Format(strImageID, "20-0000")
'Parent Path
strFolder = strParent & strFormat
'Create FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
'Check Whether Folder Exists
If fso.FolderExists(strFolder) = False Then
'If not, create it
fso.CreateFolder strFolder
End If
'Open It
Shell "C:\Windows\explorer.exe """ & strFolder & "", vbNormalFocus
*When the folder opens, this is where the user should insert a new picture. After this I want the textbox to display the full path name so it can get the name of the picture as well.
'This is where I am stuck.
Me.[txtImageName].Value = strFolder & "\" & GetFilenameFromPath(strFolder)
I tried adding a function from other Stack Overflow questions but it didn't help.
Public Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
Dim picturepath As String
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
The current code gives me C:\Desktop\Pictures\20-0012\20-0012 I need the name of the picture added without knowing what was added. Thanks in advance!