I did some reading, and came up with the following code:
Function ExistingDir(ByVal strPath As String) As Boolean
Dim oFSO As Object
If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
Set oFSO = CreateObject("Scripting.FileSystemObject")
ExistingDir = oFSO.FolderExists(strPath)
End Function
Sub CreateDir(ByVal NewFolderPath As String)
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CreateFolder (NewFolderPath) '<<=== Error line
End Sub
and they are called via the following so they are being sent the same path string:
If Not ExistingDir(ArchivePath) Then
CreateDir ArchivePath
The first checks to see if the directory exists and the second creates the directory. I keep getting the following error when I step through the code:
When I look in the Locals window I can see the value for NewFolderPath is:
"C:\Users\me\Desktop\LOCAL CPS folder\Vision 2020\191007\MTOD\Archive\20200124"
I read the following questions Q1 Q2 Q3
I have tried:
oFSO.CreateFolder (TRIM(NewFolderPath))
oFSO.CreateFolder (TRIM(NewFolderPath) & "\")
oFSO.CreateFolder TRIM(NewFolderPath)
oFSO.CreateFolder TRIM(NewFolderPath) & "\"
I also found this question though not sure if it applies. I suspected it might have something to do with spaces in the path, but changing the line to:
oFSO.CreateFolder """" & NewFolderPath & """"
It produced a bad filename error.
I have full read/write access to the folder C:\Users\me\Desktop\LOCAL CPS folder\Vision 2020\191007\MTOD
to the best of my knowledge. I created it to begin with. I am trying to create the sub folder \Archive\20020124
. And when looking though file explorer the folder does not show in the directory so its not a case of it already existing.
What am I doing wrong for creating that folder?
Update:
I think I may have figured it out. Do I need to create the folder Archive
then create the folder 20020124
?