Excel VBA调用Adobe Acrobat DC JavaScript触发438错误的解决求助
Excel VBA调用Adobe Acrobat DC JavaScript触发438错误的解决求助
各位大佬,我现在卡在一段VBA代码上了——想从Excel VBA里把JavaScript字符串传给Adobe Acrobat DC执行,结果遇到了麻烦:jsobject.app.alert这行代码能正常执行,在Acrobat里弹出提示,但到了jsObject.ExecuteJS jsScript这一行就直接抛出438错误,完全跑不起来。
我已经做了这些排查:
- 装了最新版的Excel Pro和Adobe Acrobat DC
- 分别在32位和64位机器上都试过,问题依旧
- 把要执行的JS脚本直接放到Acrobat的JavaScript控制台里测试,是完全能正常运行的,能成功给PDF添加文本注释
下面是我的代码,求各位帮忙看看哪里出问题了,万分感谢!
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
备注:内容来源于stack exchange,提问作者ChemE Stewart
相关产品推荐
相关产品推荐

