Failing to Populate Secured PDF with Excel VBA
Hi,
I have been using the same Excel VBA code below to populate a few PDFs in the past, .
However, when I try now to use the same code for a PDF that has [SECURED] in its file name, when I open it with Adobe Acrobat Pro, it won't work.
I am able to modify this PDF manualy and save the changes I make, but failing to do so with Excel VBA.
Any ideas ?
My Code
Option Explicit
' --- Adobe Acrobat Objects ---
Dim AcroApp As Acrobat.AcroApp
Dim AcroDoc As Acrobat.AcroAVDoc
Dim AcroPdf As New Acrobat.AcroPDDoc
Dim PDFJSO As Object
' ===== List of Fields in PDF =====
Const AddressFldName As String = "p_Street_Address" ' address
Const CityFldName As String = "p_City" ' city
Const ZipCodeFldName As String = "p_Zip" ' Zip Code
Sub FMLSSaleAgreementPDFPopulate()
Dim PDFFullPathAndName As String
Dim PSAFullFileName As String
' set the Full path and PDf file name (including extension)
PDFFullPathAndName = "C:\My Path\PDF Template Name.pdf"
On Error Resume Next
' initialize Acrobat by creating the App Object
Set AcroApp = CreateObject("AcroExch.App")
' Check if the Acrobat App was created
If Err.Number <> 0 Then
MsgBox "Could not create the App object!", vbCritical, "Object error"
' release the object and exit
Set AcroApp = Nothing
Exit Sub
End If
' Create the AVDoc object
Set AcroDoc = CreateObject("AcroExch.AVDoc")
' Check if the AVDoc object was created
If Err.Number <> 0 Then
MsgBox "Could not create the AVDoc object!", vbCritical, "Object error"
' release the objects and exit.
Set AcroDoc = Nothing
Set AcroApp = Nothing
Exit Sub
End If
On Error GoTo 0 ' reset Adobe object Resume Next Error handling
'--- Check if PDF Template exists in Folder ---
If Len(Dir(PDFFullPathAndName)) = 0 Then
MsgBox "Unable to find PDF template in folder", vbCritical, "PDF Template Error"
Exit Sub
End If
' open the PDF file.
If AcroDoc.Open(PDFFullPathAndName, "") = True Then
Application.ScreenUpdating = False
' set the PDDoc object
Set AcroPdf = AcroDoc.GetPDDoc
' set the JS Object - Java Script Object
Set PDFJSO = AcroPdf.GetJSObject
' === Paragraph 1 - General Info ===
With ThisWorkbook.Sheets("PDF")
PDFJSO.getField(AddressFldName).Value = "address test"
PDFJSO.getField(CityFldName).Value = "city name"
PDFJSO.getField(ZipCodeFldName).Value = "99999"
End With
End If
Application.ScreenUpdating = True
PSAFullFileName = FMLSPDFFolderPathRng.Value2 & "\" & PSAFullFileName & Format(Date, "yyyy_mm_dd") & ".pdf"
AcroPdf.Save 1, PSAFullFileName
AcroPdf.Close
AcroDoc.Close 1
' Release all Acrobat Objects
AcroApp.Exit
Set PDFJSO = Nothing
Set AcroPdf = Nothing
Set AcroDoc = Nothing
Set AcroApp = Nothing
End Sub
