将文件夹中的pdf文件名导出为ex​​cel

Str*_*ker 2 excel vba excel-vba

我已经创建了一个代码来为我提供路径,并为文件夹中的所有文件指定excel.但我的问题是它给了我该文件夹中所有文件的文件名.我只想搜索和检索只有pdf文件的名称才能出类拔萃.

这是我有的:

Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(Range("H1").Value)

i = 1

'loops through each file in the directory and prints their names and path

For Each objFile In objFolder.Files
'print file path
Cells(i + 3, 2) = objFile.Path
i = i + 1
Next objFile

End Sub
Run Code Online (Sandbox Code Playgroud)

Sco*_*ner 7

根据评论.您需要测试最后三个字符是否为'pdf'

所以在你的for循环中添加if语句

For Each objFile In objFolder.Files
    if right(objFile.Path,3) = "pdf" then
       'print file path
        Cells(i + 3, 2) = objFile.Path
        i = i + 1
    end if
Next objFile
Run Code Online (Sandbox Code Playgroud)