Wednesday, June 17, 2009

A script to search for a particular text in a PDF file using QTP.

See main post here.

Below code searches for text “Windows” in a PDF file. You can learn more about method FindText and Wait which are used in this code here and here.

Option Explicit
Dim accapp, acavdocu
Dim pdf_path, bReset, Wrd_count
pdf_path = "C:\Tips.pdf"

'AcroExch.App is the Acrobat application; below we are initializing Acrobat by creating App object

Set accapp = CreateObject( "AcroExch.App" )

'show method shows the Acrobat application


'we have to create one AVDoc object per displayed document

Set acavdocu = CreateObject( "AcroExch.AVDoc" )

' opening the PDF

If acavdocu.Open( pdf_path, "" ) Then
bReset = 1 : Wrd_count = 0
'FindText:Finds the specified text, scrolls so that it is visible, and highlights it
Do While acavdocu.FindText( "Primary", 1, 1, bReset )
bReset = 0 : Wrd_count = Wrd_count + 1
Wait 0, 200
End If

msgbox "The word 'Windows' was found " & Wrd_count & " times."
Set accapp = Nothing:Set accapp = Nothing

The above code when run shows a message box like this below: