Hello, I really hope you guys ca help me...
This macro loops through all the pdf files in the selected location and should add a custom stamp in the first page of each file. Of course it does not work!
I keep getting the runtime error 438 for jsObj.ExecuteJavaScript jsCommand
I manually applied the stamp using the JavaScript Console and it works perfectly.
Tried adding a little pause to allow the pdf to fully load as well as many little variations but nothing works.
I have Abobe Acrobat Pro 32 bits...
Thank you!!
This macro loops through all the pdf files in the selected location and should add a custom stamp in the first page of each file. Of course it does not work!
I keep getting the runtime error 438 for jsObj.ExecuteJavaScript jsCommand
I manually applied the stamp using the JavaScript Console and it works perfectly.
Tried adding a little pause to allow the pdf to fully load as well as many little variations but nothing works.
I have Abobe Acrobat Pro 32 bits...
Thank you!!
VBA Code:
Option Explicit
Sub AddSavedSignatureStampToPDFs()
Dim folderPath As String
Dim fileName As String
Dim AcroApp As Object
Dim AVDoc As Object
Dim PDDoc As Object
Dim jsObj As Object
Dim jsCommand As String
Dim newFileName As String
Dim maxRetries As Integer
Dim retries As Integer
' Prompt user to select folder containing PDFs
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select Folder Containing PDF Files"
If .Show = -1 Then
folderPath = .SelectedItems(1) & "\"
Else
MsgBox "No folder selected. Exiting."
Exit Sub
End If
End With
' Initialize Acrobat
On Error Resume Next
Set AcroApp = CreateObject("AcroExch.App")
If AcroApp Is Nothing Then
MsgBox "Could not initialize Adobe Acrobat. Make sure it's installed and accessible.", vbCritical
Exit Sub
End If
AcroApp.Show
On Error GoTo 0
' Loop through each PDF in the folder
fileName = Dir(folderPath & "*.pdf")
Do While fileName <> ""
On Error Resume Next
Set AVDoc = CreateObject("AcroExch.AVDoc")
If AVDoc.Open(folderPath & fileName, "") Then
On Error GoTo 0
' Wait for the document to fully render
maxRetries = 10 ' Number of retries
retries = 0
Do While retries < maxRetries
If AVDoc.IsValid Then Exit Do
retries = retries + 1
Application.Wait Now + TimeValue("0:00:01") ' Wait 1 second
Loop
If retries = maxRetries Then
MsgBox "Document took too long to load: " & fileName, vbExclamation
AVDoc.Close True
fileName = Dir
GoTo NextFile
End If
' Get the PDDoc and JavaScript Object
Set PDDoc = AVDoc.GetPDDoc
Set jsObj = PDDoc.GetJSObject
If jsObj Is Nothing Then
MsgBox "Failed to get JavaScript object for file: " & fileName, vbExclamation
AVDoc.Close True
fileName = Dir
GoTo NextFile
End If
' JavaScript to apply the stamp
jsCommand = "this.addAnnot({" & _
"page: 0," & _
"type: 'Stamp'," & _
"name: '6a833177-32c9-4264-b013-c39e5b959dda'," & _
"rect: [400, 400, 550, 500]" & _
"});"
' Execute the JavaScript command
jsObj.ExecuteJavaScript jsCommand
' Save the updated PDF with "_Signed" appended to the filename
newFileName = Replace(folderPath & fileName, ".pdf", "_Signed.pdf")
PDDoc.Save &H1, newFileName
' Close the document
AVDoc.Close True
Else
MsgBox "Failed to open file: " & fileName, vbExclamation
End If
NextFile:
fileName = Dir
Loop
' Cleanup
AcroApp.CloseAllDocs
AcroApp.Exit
Set AcroApp = Nothing
MsgBox "Signature stamping complete.", vbInformation, "Done"
End Sub