VH88
Aerospace
- Feb 20, 2020
- 9
Hi all,
VBA gives me "File Not Found" error when I ask it to execute "FSO.GetFile(fullFilePath)" command. The fullFilePath is set to an excel cell which is "D:\User\_Engineering\9_VBA\z_in work\HOW TO OPEN PERSONAL VBA WORKBOOK.txt". As you can see I have space in the file path "z_in work" and also in the file name "HOW TO OPEN PERSONAL VBA WORKBOOK.txt". One of the solutions I found online is reconstruct the full file path like below.
Path = FSO.GetParentFolderName(fullFilePath)
fileName = FSO.GetFileName(fullFilePath)
Strf = Split(fileName, ".")
fileNameNoExtension = Strf(0)
Extension = ".txt"
fullFilePath = Path & "\" & fileNameNoExtension & Extension
It works but I want the Extension variable to be dynamic like "Extension = "." & Strf(1)", not a string enclosed by double quote. I've also tried so many other options with double quote but got no luck so far. Below is the full testing code. Thanks in advance for your help. Hopefully, Microsoft will fix this quirk one day.
'---------------------------------------------------------------------
Sub fso_test()
Dim fullFilePath, Extension As String
Dim WStemp As Worksheet
Set WStemp = ThisWorkbook.Worksheets("temp_sheet")
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
Dim File As File
fullFilePath = WStemp.Cells(2, 1)
Path = FSO.GetParentFolderName(fullFilePath)
fileName = FSO.GetFileName(fullFilePath)
Strf = Split(fileName, ".")
fileNameNoExtension = Strf(0)
Extension = "." & Strf(1)
'Extension = ".txt" 'worked
'fullFilePath = Path & "\" & fileNameNoExtension & ".txt" 'worked
'fullFilePath = Path & "\" & fileNameNoExtension & "." & Extension
'fullFilePath = Path & "\" & fileNameNoExtension & Extension
'fullFilePath = """" & Path & "\" & fileNameNoExtension & Extension & """"
'fullFilePath = "" & fullFilePath & ""
Debug.Print fullFilePath
Set File = FSO.GetFile(fullFilePath)
End Sub
'---------------------------------------------------------------------
VBA gives me "File Not Found" error when I ask it to execute "FSO.GetFile(fullFilePath)" command. The fullFilePath is set to an excel cell which is "D:\User\_Engineering\9_VBA\z_in work\HOW TO OPEN PERSONAL VBA WORKBOOK.txt". As you can see I have space in the file path "z_in work" and also in the file name "HOW TO OPEN PERSONAL VBA WORKBOOK.txt". One of the solutions I found online is reconstruct the full file path like below.
Path = FSO.GetParentFolderName(fullFilePath)
fileName = FSO.GetFileName(fullFilePath)
Strf = Split(fileName, ".")
fileNameNoExtension = Strf(0)
Extension = ".txt"
fullFilePath = Path & "\" & fileNameNoExtension & Extension
It works but I want the Extension variable to be dynamic like "Extension = "." & Strf(1)", not a string enclosed by double quote. I've also tried so many other options with double quote but got no luck so far. Below is the full testing code. Thanks in advance for your help. Hopefully, Microsoft will fix this quirk one day.
'---------------------------------------------------------------------
Sub fso_test()
Dim fullFilePath, Extension As String
Dim WStemp As Worksheet
Set WStemp = ThisWorkbook.Worksheets("temp_sheet")
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
Dim File As File
fullFilePath = WStemp.Cells(2, 1)
Path = FSO.GetParentFolderName(fullFilePath)
fileName = FSO.GetFileName(fullFilePath)
Strf = Split(fileName, ".")
fileNameNoExtension = Strf(0)
Extension = "." & Strf(1)
'Extension = ".txt" 'worked
'fullFilePath = Path & "\" & fileNameNoExtension & ".txt" 'worked
'fullFilePath = Path & "\" & fileNameNoExtension & "." & Extension
'fullFilePath = Path & "\" & fileNameNoExtension & Extension
'fullFilePath = """" & Path & "\" & fileNameNoExtension & Extension & """"
'fullFilePath = "" & fullFilePath & ""
Debug.Print fullFilePath
Set File = FSO.GetFile(fullFilePath)
End Sub
'---------------------------------------------------------------------