在Excel表中,我有10个文件名,如:
test1,test2,test3
所有文件都是pdfs。
我需要在文件夹里搜索那些文件。
如果我在路径C:\ test1 \SOURCE找到了它,那么将它复制到我的桌面文件夹C:\User\ find。
但是它没有从源->目标文件夹复制任何文件。
Sub copyFile()
Dim objFSO As Object
Dim strFileToCopy, strOldPath As String, strNewPath As String
strOldPath = "C:\Users\SOURCE" 'Verzeichnis in dem die Datei liegt
strNewPath = "C:\Users\Destination" 'Verzeichnis in welches kopiert werden soll
With ActiveSheet
strFileToCopy = .Range("A1") 'Zelle mit dem Namen
strFileToCopy = strFileToCopy & ".pdf" 'Suffix anhängen
If Dir(strOldPath & strFileToCopy, vbNormal) <> "" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.copyFile strOldPath & strFileToCopy, strNewPath & strFileToCopy
End If
End With
Set objFSO = Nothing
End Sub其他信息:
A1中的值是test1
源路径中的pdf文件是test1.pdf
发布于 2021-10-11 09:36:49
不要手动连接路径。使用FileSystemObject (仅限Windows)方法。
Dim objFSO As Object, OldPath As String
...
Set objFSO = CreateObject("Scripting.FileSystemObject")
OldPath = objFSO.BuildPath(strOldPath, strFileToCopy)
If objFSO.FileExists(OldPath) Then
objFSO.copyFile OldPath, objFSO.BuildPath(strNewPath, strFileToCopy)
End If
...https://stackoverflow.com/questions/69523743
复制相似问题