使用 VBA 写入并保存为 PDF

Writing and Saving to a PDF with VBA

我已经能够成功写入 pdf,但现在我正在尝试保存它。我知道我需要使用 pddoc 才能保存,但我无法将其设置为与我用来写入 pdf 的 avdoc 相匹配。真正的代码有很多字段,所以我将把它缩减为一个字段并将我的内容放在下面:

Sub mysub()
'basic declarations and initializations
Dim myfullpath As String
Dim myField As String 
     myfullpath = "C:\mypdf.pdf"
     myField = "Hello"
 
'pdf overhead declarations and initializations
Dim aApp As Acrobat.AcroApp
Dim av_doc As Acrobat.AcroAVDoc
Dim pdf_form As AFORMAUTLib.AFormApp
     Set aApp = CreateObject("AcroExch.App")
     Set av_doc = CreateObject("AcroExch.AVDoc")
     Set pdf_form = CreateObject("AFORMAUT.App")

     If av_doc.Open(myfullpath, "") = True Then
'declare and initialize pdf fields
     Dim pdfField As AFORMAUTLib.Field
          Set pdfField = pdf_form.Fields("pdfField")

'set value in pdf
          pdfField = myField

'declare and initialize pddoc in order to save
     Dim PdfDoc As Acrobat.CAcroPDDoc
          Set PdfDoc = av_doc      'having trouble here 
                                   '“Run-time error ‘13’: Type mismatch”
          PdfDoc.Save PDSaveFull, myfullpath
          av_doc.Close False
          Set pdfField = Nothing
     End If
aApp.Exit
Set aApp = Nothing
Set av_doc = Nothing
Set PdfDoc = Nothing
End Sub

这对我有用:

Sub mysub()
    
    Dim myfullpath As String, myfullpath_edited As String
    Dim myField As String
    Dim aApp As Acrobat.AcroApp
    Dim av_doc As Acrobat.AcroAVDoc
    Dim pdfField As Object 'AFORMAUTLib.Field
    Dim PdfDoc As Acrobat.CAcroPDDoc
    Dim pdf_form As Object 'AFORMAUTLib.AFormApp
    
    myfullpath = "C:\Tester\mypdf.pdf"
    myfullpath_edited = "C:\Tester\mypdf_edited.pdf"
    myField = "Hello"
     
    Set aApp = CreateObject("AcroExch.App")
    Set av_doc = CreateObject("AcroExch.AVDoc")
    Set pdf_form = CreateObject("AFORMAUT.App")
    
    'aApp.Show
    If av_doc.Open(myfullpath, "") Then
        
        'av_doc.BringToFront
        Set pdfField = pdf_form.Fields("pdfField")
        pdfField.Value = myField 'set value in pdf
        
        Set PdfDoc = av_doc.GetPDDoc  '<<<<<<<<<<<<<<
        PdfDoc.Save PDSaveFull, myfullpath_edited
        av_doc.Close False
                  
    End If
    
    aApp.Exit
    Set aApp = Nothing
    Set av_doc = Nothing
    Set PdfDoc = Nothing
End Sub