I have been troubleshooting for a few hours what should be a simple bit of code. I have a sub (CopyPasteTemplate) that is called from another sub on a userform within Excel. It is designed to check for a local folder (fromFolder) and copy the folders contained within to another folder (toFolder) on the same drive. I have run it hundreds of times with no problems until today. I did add a bit of code today to the userform, but did not modify the sub in question...at least not intentionally. Two variables are passed to the sub, but I verified they are being passed correctly. I inserted a bit of troubleshooting code to see if the file paths are being stored correctly. See below.
Private Sub CopyPasteTemplate(path As String, iPos As String)
'--------------------
'Copies folders from "0_Template Job Folder" ("fromFolder") to project folder ("toFolder") being created
'--------------------
Dim FSO
Dim sFolder As String
Dim fromFolder As String
Dim toFolder As String
sFolder = "0_Project Folder Template" 'This is the folder name with the contents you want to copy
fromFolder = Left(LocalFullName(ActiveWorkbook.FullName), iPos) & sFolder & "\*" 'Location of folder(s) you want to copy"
toFolder = path 'Change to match the destination folder path
Set FSO = CreateObject("Scripting.FileSystemObject") 'Create Object
'-------------this section created to determine which path cannot be found----------------------
Debug.Print "fromFolder->" & fromFolder
Debug.Print "toFolder->" & toFolder
If FSO.FolderExists(fromFolder) = False Then
Debug.Print "fromFolder found->" & fromFolder
Else
Debug.Print "fromFolder found->" & "not found"
End If
If FSO.FolderExists(toFolder) = False Then
Debug.Print "toFolder found->" & toFolder
Else
Debug.Print "toFolder found->" & "not found"
End If
'----------------------------------------------------------------------------------------------
If Not FSO.FolderExists(Left(LocalFullName(ActiveWorkbook.FullName), iPos) & sFolder) Then 'Checking If File Is Located in the Source Folder
MsgBox "Specified Folder Not Found", vbInformation, "Not Found"
Else
FSO.CopyFolder (fromFolder), toFolder, True
MsgBox "Specified Folder Copied Successfully", vbInformation, "Done!"
End If
End Sub
As you can see by the debug printout the file path for the toFolder and fromFolder are correct, but FSO.FolderExists cannot find the toFolder path.
What am I missing? Is there anything aside from modifying the code that could have changed? Frankly I am stumped. Any advice would be appreciated.
