VBA Loop Through PDF Files in Folder

dannie

New Member
Joined
Dec 29, 2014
Messages
34
Hello All,

I am trying to loop through files in a folder and searching through the words. I have the searching words and finding what I need there. What I am having trouble with is my code is only finding one file. I appreciate any help. Maybe I putting something out of order...?

Thank you,
Dannie

Code:
            folder = "T:\USER\emt\PPA Amend\1"            myfile = Dir(folder & "\" & "*" & ".pdf")
                
                For Each cell In Sheets(1).Range("K2:K" & LR)
                    Do While myfile <> ""
                        Set pddoc = CreateObject("AcroExch.PDDoc")
                            If pddoc.Open(folder & "\" & myfile) Then
                                Set jso = pddoc.getjsobject
                                inum = pddoc.getnumpages
                                If Not jso Is Nothing Then
                                    Count = jso.getpagenumwords(0)
                                    For i = 18 To 18
                                        word = jso.getpagenthword(0, i)
                                            If LCase(word) Like (LCase(Email) & "*") Then
                                                MsgBox word
                                            End If
                                    Next
                                End If
                            End If
                    Loop
                Next
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this.
Code:
    folder = "T:\USER\emt\PPA Amend\1"
    myfile = Dir(folder & "\" & "*" & ".pdf")

    For Each cell In Sheets(1).Range("K2:K" & LR)
        Do While myfile <> ""
            Set pddoc = CreateObject("AcroExch.PDDoc")
            If pddoc.Open(folder & "\" & myfile) Then
                Set jso = pddoc.getjsobject
                inum = pddoc.getnumpages
                If Not jso Is Nothing Then
                    Count = jso.getpagenumwords(0)
                    For i = 18 To 18
                        word = jso.getpagenthword(0, i)
                        If LCase(word) Like (LCase(Email) & "*") Then
                            MsgBox word
                        End If
                    Next
                End If
            End If
            myfile = Dir
        Loop
    Next cell
 
Upvote 0
Thanks Norie. This works but instead of going to the next file in the folder, it jumps to the tenth file and then the 100th. Is there a way for it to go in order? Each file is named the same with a different integer at the end of the name. There are about 1500 files.
Thanks.
 
Upvote 0
The code should go through all the files in the folder but unfortunately there's no way to determine in which order it will do that.

If you do have some sort of naming pattern/logic then you could possibly do something with that if you require things to happen in a particular order.

Can you expand on how the files are named?
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top