Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

File System Object won't recognize file name or file path with space | File Not Found Error

Status
Not open for further replies.

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
'---------------------------------------------------------------------
 
Replies continue below

Recommended for you

Enclosing your file path in double quotes usually works.

Code:
FSO.GetFile(Chr(34) & fullFilePath & Chr(34))
or
FSO.GetFile("""" & fullFilePath & """")
 
Hi cowski. I had also tried Chr(34) and """" before, did not work for me.
 
Does the path name have to go back to the root directory?

So strange to see the singularity approaching while the entire planet is rapidly turning into a hellscape. -John Coates

-Dik
 
Below is the path I use
"D:\User\_Engineering\9_VBA\z_in work\"

And the file name is "HOW TO OPEN PERSONAL VBA WORKBOOK.txt"
 
That's pretty close to the root...

So strange to see the singularity approaching while the entire planet is rapidly turning into a hellscape. -John Coates

-Dik
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor