Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在找到搜索词后停止循环搜索?_Excel_Vba_Loops_For Loop - Fatal编程技术网

Excel 如何在找到搜索词后停止循环搜索?

Excel 如何在找到搜索词后停止循环搜索?,excel,vba,loops,for-loop,Excel,Vba,Loops,For Loop,我已经把我所有的PDF压缩成一个文件 即使在找到搜索词之后,代码仍在所有页面中循环,直到找到搜索词之后才检索页码 它跑得很慢。我想修改这段代码以运行得更快。只需打开工作簿一次 Sub FetchMultiplePDF() Dim SearchResult As String Dim i As Integer, weld As Integer, report As Integer Dim search As String, iName As String, Filepath As String,

我已经把我所有的PDF压缩成一个文件

即使在找到搜索词之后,代码仍在所有页面中循环,直到找到搜索词之后才检索页码

它跑得很慢。我想修改这段代码以运行得更快。只需打开工作簿一次

Sub FetchMultiplePDF()
Dim SearchResult As String
Dim i As Integer, weld As Integer, report As Integer
Dim search As String, iName As String, Filepath As String, FileName As String

weld = Range("Weld").Column
lrow = Cells(Rows.Count, weld).End(xlUp).Row
For i = 5 To lrow
    search = Cells(i, weld)

    FileType = Range("FileType")
    report = Range("SearchFor").Column
    iName = Cells(i, report)
    Filepath = Range("File_Path")

    FileName = (Filepath & iName & "." & FileType)

    SearchResult = AdobePdfSearch(search, FileName)
    Range("N" & i) = SearchResult
    'MsgBox SearchResult
Next i
End Sub


Function AdobePdfSearch(SearchString As String, strFileName As String) As 
String
'Note: A Reference to the Adobe Library must be set in Tools|References!
'Note! This only works with Acrobat Pro installed on your PC, will not 
work with Reader
Dim AcroApp As CAcroApp, AcroAVDoc As CAcroAVDoc, AcroPDDoc As CAcroPDDoc
Dim AcroHiliteList As CAcroHiliteList, AcroTextSelect As CAcroPDTextSelect
Dim PageNumber, PageContent, Content, i, j, iNumPages
Dim strResult As String

Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
If AcroAVDoc.Open(strFileName, vbNull) <> True Then Exit Function

Set AcroPDDoc = AcroAVDoc.GetPDDoc
iNumPages = AcroPDDoc.GetNumPages
For i = 0 To iNumPages - 1
    Set PageNumber = AcroPDDoc.AcquirePage(i)
    Set PageContent = CreateObject("AcroExch.HiliteList")
    If PageContent.Add(0, 9000) <> True Then Exit Function
    Set AcroTextSelect = PageNumber.CreatePageHilite(PageContent)
    ' The next line is needed to avoid errors with protected PDFs that can't be read
    On Error Resume Next
    For j = 0 To AcroTextSelect.GetNumText - 1
        Content = Content & AcroTextSelect.GetText(j)
    Next j
    If InStr(1, LCase(Content), LCase(SearchString)) > 0 Then
        strResult = IIf(strResult = "", i + 1, strResult & "," & i + 1)
    End If
    Content = ""
Next i

AdobePdfSearch = strResult

'Uncomment the lines below if you want to close the PDF when done.
AcroAVDoc.Close True
AcroApp.Exit
End Function
Sub-FetchMultiplePDF()
将搜索结果设置为字符串
尺寸i为整数,焊接为整数,报告为整数
Dim搜索为字符串,iName搜索为字符串,Filepath搜索为字符串,FileName搜索为字符串
焊缝=范围(“焊缝”)。列
lrow=单元格(行数,焊缝)。结束(xlUp)。行
对于i=5至lrow
搜索=单元(i,焊接)
文件类型=范围(“文件类型”)
报告=范围(“搜索”)。列
iName=单元(i,报告)
文件路径=范围(“文件路径”)
FileName=(文件路径、名称和文件类型)
SearchResult=AdobePdfSearch(搜索,文件名)
范围(“N”&i)=搜索结果
'MsgBox SearchResult
接下来我
端接头
函数AdobePdfSearch(SearchString作为字符串,strFileName作为字符串)作为
一串
'注意:必须在工具|引用中设置对Adobe库的引用!
“注意!这仅适用于安装在PC上的Acrobat Pro,不会
与读者合作
Dim AcroApp作为CAcroApp,AcroAVDoc作为CAcroAVDoc,AcroPDDoc作为CAcroPDDoc
Dim AcroHiliteList作为CAcroHiliteList,AcroTextSelect作为CAcroPDTextSelect
Dim页码、页面内容、内容、i、j、inmpage
作为字符串的Dim strResult
设置AcroApp=CreateObject(“AcroExch.App”)
设置AcroAVDoc=CreateObject(“AcroExch.AVDoc”)
如果AcroAVDoc.Open(strFileName,vbNull)为True,则退出函数
设置AcroPDDoc=AcroAVDoc.GetPDDoc
iNumPages=AcroPDDoc.GetNumPages
对于i=0到iNumPages-1
设置页码=AcroPDDoc.AcquirePage(i)
Set PageContent=CreateObject(“AcroExch.hilitlist”)
如果PageContent.Add(0,9000)为真,则退出函数
设置AcroTextSelect=PageNumber.CreatePageHilite(页面内容)
'下一行是为了避免无法读取受保护PDF的错误
出错时继续下一步
对于j=0到AcroTextSelect.GetNumText-1
Content=Content&AcroTextSelect.GetText(j)
下一个j
如果InStr(1,LCase(Content),LCase(SearchString))>0,则
strResult=IIf(strResult=“”,i+1,strResult&“,”&i+1)
如果结束
Content=“”
接下来我
AdobePdfSearch=strResult
'如果要在完成后关闭PDF,请取消对以下行的注释。
AcroAVDoc.Close为真
应用程序退出
端函数

strResult=IIf(strResult=“”,i+1,strResult&“,”&i+1)
在这一行之后,您需要将
退出,因为它从未复制页码。一旦找到,它就跳到下一行。单步遍历代码显示,j=0到AcroTextSelect.GetNumText-1在这些行上花费了相当长的时间`