Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a PDF using VBA in Excel

Tags:

excel

pdf

vba

I'm trying to open all appropriate PDFs found in the same directory as my Excel workbook using VBA. I've added the Adobe Acrobat xx.x Type Library reference to the project. But when I try to create the .App object I get a "Run-time error '429':" error.

What am I missing?

Here's the code;

Sub ImportNames()
Dim BlrInfoFileList() As String, NbrOfFiles As Integer, FileNameStr As String
Dim X As Integer, pdfApp As AcroApp, pdfDoc As AcroAVDoc


'Find all of the Contact Information PDFs
FileNameStr = Dir(ThisWorkbook.Path & "\*Contact Information.pdf")
NbrOfFiles = 0
Do Until FileNameStr = ""
    NbrOfFiles = NbrOfFiles + 1
    ReDim Preserve BlrInfoFileList(NbrOfFiles)
    BlrInfoFileList(NbrOfFiles) = FileNameStr
    FileNameStr = Dir()
Loop

For X = 1 To NbrOfFiles
    FileNameStr = ThisWorkbook.Path & "\" & BlrInfoFileList(X)
    Set pdfApp = CreateObject("AcroExch.App")
    pdfApp.Hide

    Set pdfDoc = CreateObject("AcroExch.AVDoc")
    pdfDoc.Open FileNameStr, vbNormalFocus

    SendKeys ("^a")
    SendKeys ("^c")
    SendKeys "%{F4}"

    ThisWorkbook.Sheets("Raw Data").Range("A1").Select
    SendKeys ("^v")
    Set pdfApp = Nothing
    Set pdfDoc = Nothing

    'Process Raw Data and Clear the sheet for the next PDF Document
Next X
End Sub
like image 961
user2668956 Avatar asked Oct 04 '13 15:10

user2668956


People also ask

Can Excel VBA read PDF?

Select the PDF file that you want to convert. Wait for your PDF to be converted. Once it's complete, the window will refresh, and a new spreadsheet will be opened with the data from your PDF. Well done, you can now extract data from PDF to Excel using VBA!

How do you open a PDF using Excel?

Open an Excel sheet, go to Data > Get Data. In the dropout menu, select From File > PDF File. Step 2. Select a PDF file you want to open and click Import.

How do I open a file in VBA?

To open any workbook file, we will go to the VBA page and type the code 'open wb, then we will press the enter key and type “Workbooks. Open myFile”.


1 Answers

If it's a matter of just opening PDF to send some keys to it then why not try this

Sub Sample()
    ActiveWorkbook.FollowHyperlink "C:\MyFile.pdf"
End Sub

I am assuming that you have some pdf reader installed.

like image 67
Siddharth Rout Avatar answered Oct 23 '22 20:10

Siddharth Rout