我正在嘗試制作一個程式來讀取一個 pdf 和一個 vba 用戶表單,然后填寫另一個 pdf。我成功地撰寫了代碼來讀取 pdf 中的所有文本,然后根據我可以在字串中找到的標記找到某些子字串。它旨在根據子字串填充目標 pdf 中的欄位,并根據用戶表單檢查適當的文本框。我可以獲得填充子字串的代碼,然后保存檔案,但它不會選中這些框。在代碼使用AVDoc之前,但我切換到了JSO,因為我不想彈出pdf,而jso成功地避免了這個問題。我嘗試了各種代碼方法,例如 pdfBool.value = cBool(vbaBool)、pdfBool.value = 1、pdfBool.value = "1"、jso.setValue("checked")、jso.setValue("yes")、等此代碼將運行而不會崩潰。
Sub main()
‘findString grabs all text from a pdf file. This code works.
Dim mystr As String
If findString(mystr) = False Then
Application.StatusBar = "Cannot find Source PDF"
Exit Sub
End If
Dim mypath As String
mypath = ActiveWorkbook.Path & "\destination.pdf"
Dim aApp As acrobat.AcroApp
Dim pdfDoc As acrobat.CAcroPDDoc
Dim jso As Object
Set aApp = CreateObject("AcroExch.App")
Set pdfDoc = CreateObject("AcroExch.PDDoc")
If pdfDoc.Open(mypath) = True Then
Set jso = pdfDoc.GetJSObject
Dim vbaText As String
Dim vbaBool As String
vbaText = returnString("Token1")
vbaBool = userForm.checkBox1.value
Dim pdfText As Object
Dim pdfBool As Object
Set pdfText = jso.getField("TextField1")
Set pdfBool = jso.getField("CheckBox1")
pdfText.Value = vbaText
pdfBool.Value = vbaBool
'save pdffile
Dim fileSavePath As String
fileSavePath = ActiveWorkbook.Path & "\My Save File.pdf"
pdfDoc.Save PDSaveFull, fileSavePath
‘clean up memory
Set pdfDoc = Nothing
Set pdfText = Nothing
Set pdfBool = Nothing
Set jso = Nothing
End If
aApp.Exit
Set aApp = Nothing
Unload userForm1
End Sub
uj5u.com熱心網友回復:
好的,所以經過一番搜索,我找到了解決方案。基本上,使用生命周期創建的表單不適用于復選框。我詢問了我組織中的某個人,他們確認在表單上使用了一段時間的 Living Cycle,直到我們擺脫它。老實說,我不知道生命周期是什么,但解決方案似乎有效,所以我認為無論問題與所謂的“生命周期”有關。
解決方案?重做 pdf 表單:我將 pdf 匯出到 Encapsulated PostScript 檔案。這剝奪了所有的領域。之后,我使用了自動找到所有相關欄位的準備表單工具。幸運的是,在我的 pdf 中,它完美地找到了所有欄位,盡管我必須洗掉一兩個額外的欄位。欄位名稱和代碼需要匹配,因此需要對 PDF 或代碼進行調整,但是一旦我進行了調整,一切就完美了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444820.html
