×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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
'---------------------------------------------------------------------

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

Enclosing your file path in double quotes usually works.

CODE

FSO.GetFile(Chr(34) & fullFilePath & Chr(34))
or
FSO.GetFile("""" & fullFilePath & """") 

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

(OP)
Hi cowski. I had also tried Chr(34) and """" before, did not work for me.

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

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

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

(OP)
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"

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close