Write PDF Forms - help needed

theprincipal78

Board Regular
Joined
Aug 5, 2009
Messages
68
Hi there
I wanna get straight to the problem:

I have a list of pdf field names on my excel sheet starting in cell A5.

I add values - pdf field values - to my excel sheet starting in cell B5

I want to:
Update the pdf document with the cell values on the excel sheet - starting in cell B5.

below code needs to be amended accordingly.

any help is highly appreciated.

'------------------------------------------------------------------------------------------------------------


Option Explicit


Sub Write_PDF_Forms()

'--------------------------------------------------------------------------------------
'This macro uses the data in sheet Write in order to fill a sample PDF form named
'Test Form, which is located in the same folder with this workbook. The data from
'each row is used to create a new PDF file, which is saved in the Forms subfolder.

'The code uses late binding, so no reference to external library is required.
'However, the code works ONLY with Adobe Professional, so don't try to use it with
'Adobe Reader because you will get an "ActiveX component can't create object" error.

'Written by: Christos Samaras
'Date: 15/10/2013
'e-mail: xristos.samaras@gmail.com
'site: My Engineering World
'--------------------------------------------------------------------------------------


'Declaring the necessary variables.
Dim strPDFPath As String
Dim strFieldNames(1 To 11) As String
Dim i As Long
Dim j As Integer
Dim lastRow As Long
Dim objAcroApp As Object
Dim objAcroAVDoc As Object
Dim objAcroPDDoc As Object
Dim objJSO As Object
Dim strPDFOutPath As String

'Disable screen flickering.
Application.ScreenUpdating = False

'Specify the path of the sample PDF form.
'Full path example:
'strPDFPath = "C:\Users\Christos\Desktop\Test Form.pdf"
'Using workbook path:
strPDFPath = ThisWorkbook.Path & "" & "Test Form.pdf"

'Set the required field names in the PDF form.
strFieldNames(1) = "First Name"
strFieldNames(2) = "Last Name"
strFieldNames(3) = "Street Address"
strFieldNames(4) = "City"
strFieldNames(5) = "State"
strFieldNames(6) = "Zip Code"
strFieldNames(7) = "Country"
strFieldNames(8) = "E-mail"
strFieldNames(9) = "Phone Number"
strFieldNames(10) = "Type Of Registration"
strFieldNames(11) = "Previous Attendee"

'Find the last row of data in sheet Write.
With shWrite
.Activate
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With

'Loop through all rows of sheet Write and use the data to fill the PDF form.
For i = 4 To lastRow

On Error Resume Next

'Initialize Acrobat by creating the App object.
Set objAcroApp = CreateObject("AcroExch.App")

'Check if the object was created.
If Err.Number <> 0 Then
MsgBox "Could not create the App object!", vbCritical, "Object error"
'Release the object and exit.
Set objAcroApp = Nothing
Exit Sub
End If

'Create the AVDoc object.
Set objAcroAVDoc = CreateObject("AcroExch.AVDoc")

'Check if the object was created.
If Err.Number <> 0 Then
MsgBox "Could not create the AVDoc object!", vbCritical, "Object error"
'Release the objects and exit.
Set objAcroAVDoc = Nothing
Set objAcroApp = Nothing
Exit Sub
End If

On Error GoTo 0

'Open the PDF file.
If objAcroAVDoc.Open(strPDFPath, "") = True Then

'Set the PDDoc object.
Set objAcroPDDoc = objAcroAVDoc.GetPDDoc

'Set the JS Object - Java Script Object.
Set objJSO = objAcroPDDoc.GetJSObject

On Error Resume Next

'Fill the form fields.
For j = 1 To 10

objJSO.GetField(strFieldNames(j)).Value = CStr(shWrite.Cells(i, j + 1).Value)

If Err.Number <> 0 Then

'Close the form without saving the changes.
objAcroAVDoc.Close True

'Close the Acrobat application.
objAcroApp.Exit

'Inform the user about the error.
MsgBox "The field """ & strFieldNames(j) & """ could not be found!", vbCritical, "Field error"

'Release the objects and exit.
Set objJSO = Nothing
Set objAcroPDDoc = Nothing
Set objAcroAVDoc = Nothing
Set objAcroApp = Nothing
Exit Sub

End If
Next j

'Fill the checkbox field.
If shWrite.Cells(i, j + 1).Value = "True" Then
objJSO.GetField(strFieldNames(11)).Value = "Yes"
End If

On Error GoTo 0

'Create the output path, which will be like C:\Users\Christos\Desktop\Forms\01) First Name Last Name.pdf.
With shWrite
If i - 3 < 10 Then
strPDFOutPath = ThisWorkbook.Path & "\Forms\0" & i - 3 & " " & .Cells(i, 2).Value & " " & .Cells(i, 3).Value & ".pdf"
Else
strPDFOutPath = ThisWorkbook.Path & "\Forms" & i - 3 & " " & .Cells(i, 2).Value & " " & .Cells(i, 3).Value & ".pdf"
End If
End With

'Save the form as new PDF file.
objAcroPDDoc.Save 1, strPDFOutPath

'Close the form without saving the changes.
objAcroAVDoc.Close True

'Close the Acrobat application.
objAcroApp.Exit

'Release the objects.
Set objJSO = Nothing
Set objAcroPDDoc = Nothing
Set objAcroAVDoc = Nothing
Set objAcroApp = Nothing

Else

MsgBox "Could not open the file!", vbCritical, "File error"

'Close the Acrobat application.
objAcroApp.Exit

'Release the objects and exit.
Set objAcroAVDoc = Nothing
Set objAcroApp = Nothing
Exit Sub

End If

Next i

'Enable the screen.
Application.ScreenUpdating = True

'Inform the user that forms were filled.
MsgBox "All forms were created successfully!", vbInformation, "Finished"

End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,225,322
Messages
6,184,277
Members
453,225
Latest member
adelphiaUK

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