Excel 宏停止复制文件夹。收到找不到路径的错误?
Excel macro stopped copying folders. Getting error that path cannot be found?
我已经对应该是一段简单代码的问题进行了几个小时的故障排除。
我有一个子程序 (CopyPasteTemplate),它是从 Excel 内用户窗体上的另一个子程序调用的。它旨在检查本地文件夹 (fromFolder) 并将其中包含的文件夹复制到同一驱动器上的另一个文件夹 (toFolder)。我有 运行 数百次,直到今天都没有问题。我今天确实向用户窗体添加了一些代码,但没有修改有问题的子......至少不是故意的。两个变量被传递给 sub,但我验证它们被正确传递。我插入了一些故障排除代码以查看文件路径是否正确存储。见下文。
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
正如您在调试打印输出中看到的,toFolder 和 fromFolder 的文件路径是正确的,但是 FSO.FolderExists 找不到 toFolder 路径。
我错过了什么?除了修改可能已经更改的代码之外还有什么吗?坦率地说,我很难过。如有任何建议,我们将不胜感激。
你说
If FSO.FolderExists(fromFolder) = False Then
Debug.Print "fromFolder found->" & fromFolder
'…
这意味着:“如果该文件夹不存在” 然后打印您找到它的信息。但它必须是相反的。如果存在则打印它。所以你需要检查 True
而不是 False
.
If FSO.FolderExists(fromFolder) = True Then
Debug.Print "fromFolder found->" & fromFolder
Else
Debug.Print "fromFolder found->" & "not found"
End If
If FSO.FolderExists(toFolder) = True Then
Debug.Print "toFolder found->" & toFolder
Else
Debug.Print "toFolder found->" & "not found"
End If
所以这意味着实际上找到了您的 toFolder
但没有找到您的 fromFolder
。那是因为它最后包含 \*
。删除它,它也会被发现。
调查
我在 C:\Temp\test folder
中创建了 2 个文件夹:
"C:\Temp\test folder"
(包含一些要复制的数据)
"C:\Temp\test folder -"
(空目的地,路径在破折号后结束!)
然后我设置如下测试
Sub test()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim FromFolder As String
FromFolder = "c:\temp\test folder"
Dim ToFolder As String
ToFolder = "c:\temp\test folder - " 'the path has trailing spaces after the dash so it should not exist on the disk!
Debug.Print "Found FromFolder:", FSO.FolderExists(FromFolder), """" & FromFolder & """"
Debug.Print "Found ToFolder: ", FSO.FolderExists(ToFolder), """" & ToFolder & """"
FSO.CopyFolder FromFolder, ToFolder, True
End Sub
结果是
Found FromFolder: Wahr "c:\temp\test folder"
Found ToFolder: Wahr "c:\temp\test folder - "
所以实际上 FSO.FolderExists(ToFolder)
returns True
即使这个文件夹实际上并不存在。看起来它忽略了尾随空格并找到确实存在的 "c:\temp\test folder -"
(没有空格)!
同时 FSO.CopyFolder FromFolder, ToFolder, True
失败并显示 “找不到路径”。看起来确实 not 忽略尾随空格![=28=]
所以我建议执行以下 ToFolder = Trim(ToFolder)
到 trim 关闭路径末尾或开头的所有空格。或者在你的代码中 toFolder = Trim(path)
.
我已经对应该是一段简单代码的问题进行了几个小时的故障排除。 我有一个子程序 (CopyPasteTemplate),它是从 Excel 内用户窗体上的另一个子程序调用的。它旨在检查本地文件夹 (fromFolder) 并将其中包含的文件夹复制到同一驱动器上的另一个文件夹 (toFolder)。我有 运行 数百次,直到今天都没有问题。我今天确实向用户窗体添加了一些代码,但没有修改有问题的子......至少不是故意的。两个变量被传递给 sub,但我验证它们被正确传递。我插入了一些故障排除代码以查看文件路径是否正确存储。见下文。
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
正如您在调试打印输出中看到的,toFolder 和 fromFolder 的文件路径是正确的,但是 FSO.FolderExists 找不到 toFolder 路径。
我错过了什么?除了修改可能已经更改的代码之外还有什么吗?坦率地说,我很难过。如有任何建议,我们将不胜感激。
你说
If FSO.FolderExists(fromFolder) = False Then
Debug.Print "fromFolder found->" & fromFolder
'…
这意味着:“如果该文件夹不存在” 然后打印您找到它的信息。但它必须是相反的。如果存在则打印它。所以你需要检查 True
而不是 False
.
If FSO.FolderExists(fromFolder) = True Then
Debug.Print "fromFolder found->" & fromFolder
Else
Debug.Print "fromFolder found->" & "not found"
End If
If FSO.FolderExists(toFolder) = True Then
Debug.Print "toFolder found->" & toFolder
Else
Debug.Print "toFolder found->" & "not found"
End If
所以这意味着实际上找到了您的 toFolder
但没有找到您的 fromFolder
。那是因为它最后包含 \*
。删除它,它也会被发现。
调查
我在 C:\Temp\test folder
中创建了 2 个文件夹:
"C:\Temp\test folder"
(包含一些要复制的数据)"C:\Temp\test folder -"
(空目的地,路径在破折号后结束!)
然后我设置如下测试
Sub test()
Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim FromFolder As String
FromFolder = "c:\temp\test folder"
Dim ToFolder As String
ToFolder = "c:\temp\test folder - " 'the path has trailing spaces after the dash so it should not exist on the disk!
Debug.Print "Found FromFolder:", FSO.FolderExists(FromFolder), """" & FromFolder & """"
Debug.Print "Found ToFolder: ", FSO.FolderExists(ToFolder), """" & ToFolder & """"
FSO.CopyFolder FromFolder, ToFolder, True
End Sub
结果是
Found FromFolder: Wahr "c:\temp\test folder"
Found ToFolder: Wahr "c:\temp\test folder - "
所以实际上 FSO.FolderExists(ToFolder)
returns True
即使这个文件夹实际上并不存在。看起来它忽略了尾随空格并找到确实存在的 "c:\temp\test folder -"
(没有空格)!
同时 FSO.CopyFolder FromFolder, ToFolder, True
失败并显示 “找不到路径”。看起来确实 not 忽略尾随空格![=28=]
所以我建议执行以下 ToFolder = Trim(ToFolder)
到 trim 关闭路径末尾或开头的所有空格。或者在你的代码中 toFolder = Trim(path)
.