Extract pdf text to excel

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
153
Office Version
  1. 365
Platform
  1. Windows
I got the below code from How to Extract Specific Data from PDF to Excel Using VBA - ExcelDemy
Works great but the pdf path will vary.
How can I have the user get a popup to select which file should get extracted, also is it possible to make this work on multiple files/have the user select multiple files at once.

VBA Code:
Sub Extract_Data_from_PDF()

Set MyWorksheet = ActiveWorkbook.Worksheets("Sheet1")
Application_Path = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
PDF_Path = "C:\Users\Myname\Downloads\Statement_05_2022.pdf"

Shell_Path = Application_Path & " """ & PDF_Path & """"
Call Shell(pathname:=Shell_Path, windowstyle:=vbNormalFocus)

Application.Wait Now + TimeValue("0:00:03")

SendKeys "%vpc"
SendKeys "^a"
SendKeys "^c"

MyWorksheet.Range("A1").PasteSpecial Paste:=xlPasteAll

Call Shell("TaskKill /F /IM Acrobat.exe", vbHide)

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I'm afraid I'd only do this extract in extremis - we tell our suppliers to provide Excel files as well as the PDF. If you really have to do it I'd explore the new capability within PowerQuery to extract data from PDFs.

Apologies, not the answer requested, but it would be my proposal.

All the best.
 
Upvote 0
I'm not asking about extraction suggestions. Believe me I exhausted all other options.
I need to simply know how to get a popup so the pdf_path variable is based on user input.
bonus is if anyone knows how to make this work on multiple files.
 
Upvote 0
This piece of code might help - you'll probably need to adapt it as I use it to capture the path as a hyperlink to the activecell on a sheet. After the Application.GetOpenFileName function returns sFile has the relevant string.

VBA Code:
Sub GetFileShortcut()
  Dim sFile As String
  Dim sPath As String
  Dim sName As String

  sFile = Application.GetOpenFilename(, , "Get Link", , False)
  If sFile <> "" Then
    sPath = Left(sFile, InStrRev(sFile, "\"))
    sName = Right(sFile, Len(sFile) - InStrRev(sFile, "\"))
    ActiveSheet.Hyperlinks.Add anchor:=ActiveCell, Address:=sPath, TextToDisplay:=sName
  End If
End Sub

HTH
 
Upvote 0
Solution
Not really what I'm looking for.
I need something like Application.FindFile but this simply opens the file.
I need a the path string, the file should remain closed.
 
Upvote 0
you have'nt tried it - thats exactly what you get in sPath
 
Upvote 0
My bad 🙃

PDF_Path = sPath & sName does the trick
Any way to select multiple files like this?
Then I can loop thru the next part of the code for each file separately
 
Upvote 0
Also, would you know how to edit this line Call Shell("TaskKill /F /IM Acrobat.exe", vbHide)
it should only close the file that was opened and not the entire acrobat?
 
Upvote 0
Gosh I hope you can tell I'm low on gas caffeine and sleep 🤭
sFile=Application.GetOpenFilename(, , "Get Link", , False) is the only part of your code that I need
 
Last edited:
Upvote 0
Below is my edited code that does everything I need it to including the bonus. I'm just posting in case anyone finds it helpful.
@pjmorris I marked your post as the solution to give you credit for leading me to this.

VBA Code:
Sub Extract_Data_from_PDF()
Dim App_Path As String
Dim Pdf_path As Variant
Dim shell_path As String
Dim i As Variant
Dim lastrow As Long
        
    App_Path = "C:\Program Files (x86)\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
    Pdf_path = Application.GetOpenFilename("Pdf Files (*.pdf), *.pdf", , "Select Statements", , True)
    
    For i = LBound(Pdf_path) To UBound(Pdf_path)
    
        shell_path = App_Path & " """ & Pdf_path(i) & """"
        Call Shell(pathname:=shell_path, windowstyle:=vbNormalFocus)
        
        Application.Wait Now + TimeValue("0:00:03")
        
        SendKeys "%vpc"
        SendKeys "^a"
        SendKeys "^c"
    
        With ActiveSheet
            lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        Range("a" & lastrow + 1).Select
        ActiveSheet.PasteSpecial Format:="Text"
    
    Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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