首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索PDF文件并复制到文件夹

搜索PDF文件并复制到文件夹
EN

Stack Overflow用户
提问于 2021-10-11 09:15:15
回答 1查看 302关注 0票数 0

在Excel表中,我有10个文件名,如:

test1,test2,test3

所有文件都是pdfs。

我需要在文件夹里搜索那些文件。

如果我在路径C:\ test1 \SOURCE找到了它,那么将它复制到我的桌面文件夹C:\User\ find。

但是它没有从源->目标文件夹复制任何文件。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-11 09:36:49

不要手动连接路径。使用FileSystemObject (仅限Windows)方法。

代码语言:javascript
复制
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
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69523743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档