【问题标题】:Automation of PDF String Search using Excel VBA - OLE error使用 Excel VBA 自动搜索 PDF 字符串 - OLE 错误
【发布时间】:2017-03-18 04:50:30
【问题描述】:

我在尝试自动执行 PDF 字符串搜索并在 Excel 中记录结果时收到此错误,“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

【问题讨论】:

  • 请以正确的格式发布您的代码。
  • 对此感到抱歉...我对这个网站很陌生。正在为html而苦苦挣扎

标签: vba excel pdf


【解决方案1】:

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

我使用的是 Acrobat Pro,每当我打开 PDF 文件时,由于受保护的视图设置,它打开时功能有限。如果我禁用此功能或单击启用所有功能并保存对 PDF 文件的更改,VBA 宏将运行顺利。

这很有趣,我正在发布我自己问题的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多