How to Open a PDF from Excel VBA and Copy Data
Question details
The user needs to open a PDF file via Excel VBA and copy its data into a spreadsheet, but standard commands like Shell, AppActivate, and SendKeys are proving unreliable.

- Product
- Excel VBA
- Device & OS
- not provided
- Scenario
- Automating the extraction of data from a PDF document into an Excel workbook using VBA macros.
- Observed behavior
- AppActivate and SendKeys fail to reliably focus the PDF window to copy the data, and there is uncertainty regarding which Adobe library references are required.
Verify the exact file paths for your PDF viewer executable and target PDF document. Confirm whether you are using a standard PDF reader or a professional PDF editor, as this dictates which automation capabilities are available to VBA.
Use the Shell Function and Wait Command to Open the PDF
Use the Shell function combined with an application wait timer to reliably launch and activate the PDF window.
While using Shell to launch a PDF reader and AppActivate to bring it to the front is a standard approach, relying on SendKeys to copy data is highly unreliable due to unpredictable window focus shifts. If you just need to open the document reliably, this method works well.
Ensure both the PDF reader executable path and the target PDF file path are enclosed in double quotes within your VBA code.
Use code like `id = Shell("""C:\path\to\AcroRd32.exe"" ""C:\path\to\file.pdf""", vbNormalFocus)` to launch the reader as a variable.
Insert `Application.Wait Now + TimeValue("0:00:02")` to pause the script, giving the PDF application enough time to load.
Execute `AppActivate id` to bring the newly opened PDF window into focus.

Extract Data Using COM Automation
Instead of simulating keystrokes in a free reader, use the robust COM automation libraries available in professional PDF editors.
Convert PDF to Excel Directly with WPS Office
Instead of writing complex and unreliable VBA scripts to extract data via SendKeys, you can use the built-in PDF to Excel conversion tool in WPS Office. It accurately detects tables and text, migrating your data seamlessly without requiring any code.
- 1. Open WPS Office: Launch WPS Office and navigate to the PDF tab.
- 2. Select PDF to Excel: Click on 'PDF to Excel' from the toolbar tools menu.
- 3. Add your file: Upload the PDF document you want to extract data from.
- 4. Convert and save: Click 'Convert'. The extracted data will automatically open in a new spreadsheet, ready for editing and saving in .xlsx format.

Frequently Asked Questions
Why does AppActivate fail to focus on my PDF window?
AppActivate fails if the PDF application takes longer to load than the VBA script expects, or if the process ID is not correctly passed. Adding a delay using Application.Wait helps ensure the application is ready before the script tries to focus it.
Which Adobe library reference should I use in Excel VBA?
If you have Adobe Acrobat Pro installed, you typically reference the 'Acrobat' type library. Do not select references randomly; free versions like Acrobat Reader do not expose these automation interfaces, meaning the reference will not help.
Is SendKeys reliable for copying data from other applications?
No, SendKeys is generally considered unreliable. It simply mimics keyboard presses at the OS level, meaning if a user clicks elsewhere or a background task pops up, the keystrokes will be sent to the wrong window, causing data loss or unexpected behavior.
Can I use VBA to extract text from a scanned PDF?
VBA cannot natively extract text from an image or scanned PDF. You must first use an OCR (Optical Character Recognition) tool to convert the scanned images into selectable text before extracting it via automation or conversion tools.




