VBA从PDF复制文本字符串

时间:2016-10-04 12:10:50

标签: vba pdf

我已开始使用以下代码识别PDF中的文本字符串。它工作得很好,但是,我想知道是否有办法在找到文本后将PDF中的整行复制到excel中?我不太熟悉使用VBA代码从PDF中提取,所以我现在有点卡住了。任何帮助表示赞赏!!

Sub AcrobatFindText2() 

'variables
Dim Resp 'For message box responses
Dim gPDFPath As String
Dim sText As String 'String to search for
Dim sStr As String 'Message string
Dim foundText As Integer 'Holds return value from "FindText" method

'hard coding for a PDF to open, it can be changed when needed.
gPDFPath = "C:\Users\Me\Documents\test.pdf"

'Initialize Acrobat by creating App object
Set gApp = CreateObject("AcroExch.App", "")
gApp.Hide

'Set AVDoc object
Set gAvDoc = CreateObject("AcroExch.AVDoc")

' open the PDF
If gAvDoc.Open(gPDFPath, "") Then
sText = "Designation"
'FindText params: StringToSearchFor, caseSensitive (1 or 0), WholeWords (1 or 0), 'ResetSearchToBeginOfDocument (1 or 0)
foundText = gAvDoc.FindText(sText, 1, 0, 1) 'Returns -1 if found, 0 otherwise

Else ' if failed, show error message
Resp = MsgBox("Cannot open" & gPDFPath, vbOKOnly)

End If

If foundText = -1 Then

'compose a message
sStr = "Found " & sText
Resp = MsgBox(sStr, vbOKOnly)
Else ' if failed, 'show error message
Resp = MsgBox("Cannot find" & sText, vbOKOnly)
End If

gApp.Show
gAvDoc.BringToFront

End Sub

0 个答案:

没有答案