将文件资源管理器搜索中的文件名提取到 Excel 中

owh*_*eel 4 pdf excel vba

这已经困扰我一段时间了,因为我觉得我的拼图很少,但我无法将它们全部放在一起

因此,我的目标是能够在给定位置搜索所有 .pdf 文件内容中的关键字或短语(而不是文件名),然后使用搜索结果填充 Excel 电子表格。

Before we start, I know that this easy to do using the Acrobat Pro API, but my company are not going to pay for licences for everyone so that this one macro will work.

The windows file explorer search accepts advanced query syntax and will search inside the contents of files assuming that the correct ifilters are enabled. E.g. if you have a word document called doc1.docx and the text inside the document reads "blahblahblah", and you search for "blah" doc1.docx will appear as the result. As far as I know, this cannot be acheived using the FileSystemObject, but if someone could confirm either way that would be really useful?

I have a simple code that opens an explorer window and searches for a string within the contents of all files in the given location. Once the search has completed I have an explorer window with all the files required listed. How do I take this list and populate an excel with the filenames of these files?

dim eSearch As String
eSearch = "explorer " & Chr$(34) & "search-ms://query=System.Generic.String:" & [search term here] & "&crumb=location:" & [Directory Here] & Chr$(34)
Call Shell (eSearch)
Run Code Online (Sandbox Code Playgroud)

Ale*_* K. 5

假设该位置已建立索引,您可以直接使用 ADO 访问目录(添加对 Microsoft ActiveX 数据对象 2.x 的引用):

Dim cn  As New ADODB.Connection
Dim rs  As New ADODB.Recordset
Dim sql As String

cn.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows'"

sql = "SELECT System.ItemNameDisplay, System.ItemPathDisplay FROM SystemIndex WHERE SCOPE='file:C:\look\here' AND System.Kind <> 'folder' AND CONTAINS(System.FileName, '""*.PDF""') AND CONTAINS ('""find this text""')"

rs.Open sql, cn, adOpenForwardOnly, adLockReadOnly

If Not rs.EOF Then
    Do While Not rs.EOF
        Debug.Print "File: "; rs.Collect(0)
        Debug.Print "Path: "; rs.Collect(1)
        rs.MoveNext
    Loop
End If
Run Code Online (Sandbox Code Playgroud)