Excel 如何使用VBA在.pdf中搜索字符串并返回页码

Excel 如何使用VBA在.pdf中搜索字符串并返回页码,excel,pdf,Excel,Pdf,我需要开发一个工具,将pdf文件和字符串数组作为输入,并返回找到“x”字符串的页码 到目前为止,我已经能够在pdf中搜索字符串,但我找不到返回其所在页码的方法 Sub SearchStringInPDF(searchString As String) Dim PDF_path As String Dim PathName As String Dim pdfName As String Dim waitTime As Date PathName = [Path_name] pdfName =

我需要开发一个工具,将pdf文件和字符串数组作为输入,并返回找到“x”字符串的页码

到目前为止,我已经能够在pdf中搜索字符串,但我找不到返回其所在页码的方法

Sub SearchStringInPDF(searchString As String)

Dim PDF_path As String
Dim PathName As String
Dim pdfName As String
Dim waitTime As Date

PathName = [Path_name]
pdfName = [pdf_name]

If PathName = vbNullString Then
    FullPath = ThisWorkbook.Path & "\" & pdfName
Else
    If Right(PathName, 1) <> "\" Then PathName = PathName & "\"
    PDF_path = PathName & pdfName
End If

Application.DisplayAlerts = False


'Open the pdf file
ThisWorkbook.FollowHyperlink PDF_path

'Wait time for a couple of seconds for the PDF file to fully load
waitTime = Now

'Send "CTRL + F" keys to the opened PDF to invoke find within that file
'The second argument "true" implies that Excel will wait for the keys to be processed
'before returning control to the macro.
SendKeys "^f", True

'Type the search string in the find box that opens
SendKeys searchString, True

'Hit enter to perform the s
SendKeys "{Enter}", True

Application.DisplayAlerts = True
子SearchStringInPDF(searchString作为字符串)
将PDF_路径设置为字符串
将路径名设置为字符串
Dim pdfName作为字符串
将等待时间设置为日期
路径名=[路径名称]
pdfName=[pdf_名称]
如果路径名=vbNullString,则
FullPath=ThisWorkbook.Path&“\”&pdfName
其他的
如果正确(路径名,1)“\”则路径名=路径名&“\”
PDF_path=PathName和pdfName
如果结束
Application.DisplayAlerts=False
'打开pdf文件
ThisWorkbook.FollowHyperlink PDF\u路径
'等待几秒钟,PDF文件才能完全加载
等待时间=现在
'将“CTRL+F”键发送到打开的PDF以调用该文件中的查找
第二个参数“true”表示Excel将等待处理键
'然后将控件返回到宏。
SendKeys“^f”,真
'在打开的“查找”框中键入搜索字符串
SendKeys搜索字符串,True
'按enter键执行s
SendKeys“{Enter}”,真
Application.DisplayAlerts=True
端接头

例如: 为此 还有这些弦[“acrobat蒸馏器”,“避免迷失方向”,“欧洲核子研究所”]

预期产出应为:

[第1页、第3页、第2页]

你试过了吗