Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Excel VBA error 438 calling Adobe Acrobat Pro DC Javascript

New Here ,
Dec 30, 2024 Dec 30, 2024

I got stumped on the attached VBA code trying to pass a javascript string from VBA to Adobe. The javascript "jsobject.app.alert" message executes fine and pops up in Adobe, but the "jsobject.ExecuteJS jsScript" line does not execute and throws error message 438. ChatGPT has got me this far, but I can't seem to get past this error. I have the latest versions of Excel Pro and Adobe Acrobat DC installed and I have tried on both 32-bit and 64-bit machines. I have tested the jscript string in the Acrobat javascript console and it works fine. Any help would be appreciated.

 

VBA Adobe Error.png

 

Sub AddTextToPDF()
Dim acroApp As Object
Dim acroAVDoc As Object
Dim acroPDDoc As Object
Dim jsObject As Object
Dim pdfPath As String
Dim jsScript As String
' Path to the PDF file
pdfPath = "E:\test.pdf"
' Check if the PDF file exists
If Dir(pdfPath) = "" Then
MsgBox "PDF file not found: " & pdfPath, vbExclamation, "Error"
Exit Sub
End If
' Create an Acrobat application object
On Error Resume Next
Set acroApp = CreateObject("AcroExch.App")
Set acroAVDoc = CreateObject("AcroExch.AVDoc")
On Error GoTo 0
If acroApp Is Nothing Or acroAVDoc Is Nothing Then
MsgBox "Adobe Acrobat is not installed or not available.", vbExclamation, "Error"
Exit Sub
End If
' Open the PDF file
If acroAVDoc.Open(pdfPath, "") Then
' Get the PDDoc object from the AVDoc
Set acroPDDoc = acroAVDoc.GetPDDoc
' Get the JavaScript object
Set jsObject = acroPDDoc.GetJSObject
'jsObject.app.alert "JavaScript is enabled and working!" ' this code is working
' JavaScript to add a text annotation
jsScript = "this.addAnnot({type: 'Text', page: 0, rect: [100, 500, 200, 550], contents: 'Finally, it works!'});"
' Execute the JavaScript
jsObject.ExecuteJS jsScript
' Save the changes to the PDF
acroPDDoc.Save &H1, pdfPath ' &H1 indicates incremental save
MsgBox "Text annotation added successfully!", vbInformation, "Success"
' Close the PDF
acroAVDoc.Close True
Else
MsgBox "Failed to open the PDF file.", vbExclamation, "Error"
End If

' Quit Acrobat
acroApp.Exit
Set acroPDDoc = Nothing
Set acroAVDoc = Nothing
Set acroApp = Nothing
Set jsObject = Nothing
End Sub

1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2024 Dec 30, 2024

The method ExecuteJS doesn't exists for jsObject.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2024 Dec 30, 2024

There is a function in Core JavaScript named "eval()" that will run a script in a string. 

However, your code uses a poor methodology.  A better way to create cross platform IAC code is to write the JavaScript side as an Acrobat folder level function, then call the fucntion from the JSO in your VBA code. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2024 Dec 30, 2024

[Question moved to the Acrobat SDK forum]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 08, 2025 Mar 08, 2025

i am also facing the same issue.. can anyone help

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2025 Mar 08, 2025
LATEST

By same issue, do you mean that you are trying to call a JavaScript function that does not exist in the Acrobat API?

If not, then please be more specific.  

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines