使用Excel VBA自动搜索PDF字符串- OLE错误



当尝试在Excel中自动搜索PDF字符串并记录结果时,我得到这个错误,"Microsoft Excel正在等待另一个应用程序完成OLE操作"。对于某些pdf,此错误不会弹出。我认为这是由于优化程度较低的pdf在逐页索引时需要花费较长的时间来搜索字符串。

更准确地说,我有一个包含两个工作表的工作簿。其中一个包含PDF文件名列表,另一个包含我想要搜索的单词列表。宏将从文件列表中打开每个PDF文件,从单词列表中取出每个单词并执行字符串搜索。如果找到,它会将每个发现记录在同一个工作簿的新工作表中,并带有文件名和找到的字符串。

下面是我正在努力的代码。欢迎任何帮助。

Public Sub SearchWords()
'variables
Dim ps As Range
Dim fs As Range
Dim PList As Range 
Dim FList As Range 
Dim PLRow As Long 
Dim FLRow As Long 
Dim Tracker As Worksheet
Dim gapp As Object 
Dim gAvDoc As Object 
Dim gPDFPath As String 
Dim sText As String 'String to search for 
FLRow = ActiveWorkbook.Sheets("List Files").Range("B1").End(xlDown).Row 
PLRow = ActiveWorkbook.Sheets("Prohibited Words").Range("A1").End(xlDown).Row
Set PList = ActiveWorkbook.Sheets("Prohibited Words").Range("A2:A" & PLRow) 
Set FList = ActiveWorkbook.Sheets("List Files").Range("B2:B" & FLRow) 
Set Tracker = ActiveWorkbook.Sheets("Tracker")
'For each PDF file list in Excel Range
For Each fs In FList
'Initialize Acrobat by creating App object 
Set gapp = CreateObject("AcroExch.App")
'Set AVDoc object 
Set gAvDoc = CreateObject("AcroExch.AVDoc")
'Set PDF file path to open in PDF
gPDFPath = fs.Cells.Value
' open the PDF 
If gAvDoc.Open(gPDFPath, "") = True Then
'Bring the PDF to front
gAvDoc.BringToFront
'For each word list in the range
For Each ps In PList
'Assign String to search
sText = ps.Cells.Value
'This is where the error is appearing 
If gAvDoc.FindText(sText, False, True, False) = True Then
'Record findings
Tracker.Range("A1").End(xlDown).Offset(1, 0) = fs.Cells.Offset(0, -1).Value 
Tracker.Range("B1").End(xlDown).Offset(1, 0) = ps.Cells.Value
End If
Next
End If
'Message to display once the search is over for a particular PDF
MsgBox (fs.Cells.Offset(0, -1).Value & " assignment complete")
Next

gAvDoc.Close True 
gapp.Exit
set gAVDoc = Nothing
set gapp = Nothing
End Sub

我现在找到了这个问题的答案。

我正在使用acrobatpro,每当我打开PDF文件时,由于受保护视图设置,它以有限的功能打开。如果我禁用此功能,或者如果我点击启用所有功能并保存更改到PDF文件,VBA宏运行顺利。

这是有趣的,我张贴一个答案,我自己的问题。

最新更新