Unable to create new folder with SystemFileObject in VBA

252 Views Asked by At

I'm trying to create a new folder in the same folder that my workbook is in, but I keep running into run-time error '52': "Bad file name or number". Does somebody have an idea on how to fix this?

Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CreateFolder (ThisWorkbook.Path & "\Test")

I've tried using the absolute path, but that didn't work. The "ThisWorkbook.Path" does work in a different part of my code. I've also tried forward slash and backslash, but that doesn't make any difference.

1

There are 1 best solutions below

0
Suraido On

Just in case anybody ever reads this and is in a similar situation: I think the issue was that I'm making this Excel document in a SharePoint. Excel doesn't seem to know what to do with the relative file path that SharePoint files get. The workaround I made is as follows:

Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")

Dim UserName As String
UserName = VBA.Environ("username")

Dim sourceFilePath As String
sourceFilePath = "C:\Users\" & UserName & "\folder\folder\folder"

FSO.CreateFolder (sourceFilePath & "\Test")

I had to change the folder names because it is a work-related project.