VBA Autofill Textboxes Datas From a PDF File

Tayl4n

Board Regular
Joined
Feb 17, 2021
Messages
84
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

First of all I sincerely wish you all a wonderful new year.

My question is:

I have a Word userform and it works clearly. We fill the data in the textbox and combobox manually by looking at a pdf file. I would like to add one more button like "select pdf file" then I want to select the pdf file and automatically pull the necessary data into the boxes. Is this possible? If it is could you please help me with example.

Note: PDF files are standardized and contain technical information about a product.
Ex:
Weight 10 Kg
Length: 20 M
.
.
.
goes like this.

And textboxes like:

weight of the product:
length of the product:


Kindly regards,
T
 
Last edited:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Yes, it is possible to add a button to your Word userform that allows you to select a PDF file and extract data from it. Here is an example of how you could do this using a combination of the File Dialog object and the Adobe Acrobat COM object:

  1. First, you will need to add a reference to the "Adobe Acrobat XX.X Type Library" (where XX.X is the version of Acrobat you have installed) in your Word VBA project. To do this, go to the "Tools" menu in the VBA editor, select "References," and check the box next to the Adobe Acrobat library.
  2. Next, add a button to your userform and give it a meaningful caption, such as "Select PDF File."
  3. Double-click the button to open the code editor and add the following code:
Private Sub btnSelectPdf_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker)

' Set filter to only show PDF files fd.Filters.Clear fd.Filters.Add "PDF Files", "*.pdf"

' Show the file picker and exit if the user clicks Cancel If fd.Show = False Then Exit Sub

' Open the selected PDF file using the Acrobat COM object Dim acroApp As AcroApp Dim acroAVDoc As AcroAVDoc Dim acroPDDoc As AcroPDDoc Set acroApp = CreateObject("AcroExch.App") Set acroAVDoc = CreateObject("AcroExch.AVDoc") If acroAVDoc.Open(fd.SelectedItems(1), "") Then Set acroPDDoc = acroAVDoc.GetPDDoc Else MsgBox "Error opening PDF file" Exit Sub End If

' Extract data from the PDF file and populate the form fields txtWeight.Value = acroPDDoc.GetInfo("Weight") txtLength.Value = acroPDDoc.GetInfo("Length")

' Close the PDF document and release the COM objects acroAVDoc.Close True Set acroPDDoc = Nothing Set acroAVDoc = Nothing Set acroApp = Nothing End Sub

This code uses the File Dialog object to allow the user to select a PDF file, and then opens the file using the Adobe Acrobat COM object. It then extracts the desired data from the PDF using the GetInfo method of the AcroPDDoc object, and populates the form fields with the extracted data. Finally, it closes the PDF file and releases the COM objects.
 
Upvote 0
Yes, it is possible to add a button to your Word userform that allows you to select a PDF file and extract data from it. Here is an example of how you could do this using a combination of the File Dialog object and the Adobe Acrobat COM object:

  1. First, you will need to add a reference to the "Adobe Acrobat XX.X Type Library" (where XX.X is the version of Acrobat you have installed) in your Word VBA project. To do this, go to the "Tools" menu in the VBA editor, select "References," and check the box next to the Adobe Acrobat library.
  2. Next, add a button to your userform and give it a meaningful caption, such as "Select PDF File."
  3. Double-click the button to open the code editor and add the following code:
Private Sub btnSelectPdf_Click() Dim fd As FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker)

' Set filter to only show PDF files fd.Filters.Clear fd.Filters.Add "PDF Files", "*.pdf"

' Show the file picker and exit if the user clicks Cancel If fd.Show = False Then Exit Sub

' Open the selected PDF file using the Acrobat COM object Dim acroApp As AcroApp Dim acroAVDoc As AcroAVDoc Dim acroPDDoc As AcroPDDoc Set acroApp = CreateObject("AcroExch.App") Set acroAVDoc = CreateObject("AcroExch.AVDoc") If acroAVDoc.Open(fd.SelectedItems(1), "") Then Set acroPDDoc = acroAVDoc.GetPDDoc Else MsgBox "Error opening PDF file" Exit Sub End If

' Extract data from the PDF file and populate the form fields txtWeight.Value = acroPDDoc.GetInfo("Weight") txtLength.Value = acroPDDoc.GetInfo("Length")

' Close the PDF document and release the COM objects acroAVDoc.Close True Set acroPDDoc = Nothing Set acroAVDoc = Nothing Set acroApp = Nothing End Sub

This code uses the File Dialog object to allow the user to select a PDF file, and then opens the file using the Adobe Acrobat COM object. It then extracts the desired data from the PDF using the GetInfo method of the AcroPDDoc object, and populates the form fields with the extracted data. Finally, it closes the PDF file and releases the COM objects.
Dear hydraulicwave,

I can't thank you enough. I can choose a PDF file and I'm feeling getting close to this purpose thanks to you. But after the choosing PDF I'm facing with an error and debug showing this part Set acroApp = CreateObject("AcroExch.App")
It is probably about tools and references part but I check the boxes all relative with adobe. Do you know which box I missed ?
 
Upvote 0
Dear hydraulicwave,

I can't thank you enough. I can choose a PDF file and I'm feeling getting close to this purpose thanks to you. But after the choosing PDF I'm facing with an error and debug showing this part Set acroApp = CreateObject("AcroExch.App")
It is probably about tools and references part but I check the boxes all relative with adobe. Do you know which box I missed ?
Run-time error '-2146959355 (80080005)': No such interface supported.
 
Upvote 0
I updated Adobe Acrobat Reader to Pro. Error gone but still it is not working. It is opening the pdf file and close too quickly but textboxes are remain empty please help.
 
Upvote 0
I found out that codes extract metada from pdf and works fine. But my datas inside the texts not from metada. So if I do this txtLength.Value = acroPDDoc.GetInfo("Producer") then textbox filling as "Xyz". But I can't get the necessary information from here, such as length and weight.

Title
Subject
Keywords
Author
Creator
ProducerXyz
CreationDateFri Dec 23 17:51:01 2022
ModDateFri Dec 23 17:55:55 2022
Taggedno
UserPropertiesno
Suspectsno
Formnone
JavaScriptno
Pages2
Encryptedno
Page size595 x 842 pts (A4)
Page rot0
File size317234 bytes
Optimizedno
PDF version1.4
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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