Home > OS >  Writing and Saving to a PDF with VBA
Writing and Saving to a PDF with VBA

Time:02-25

I have been able to successfully write to a pdf, but now I am trying to save it. I know I need to use a pddoc in order to save, but I can't set it to match the avdoc that I use to write to the pdf. The real code has a lot of fields so I will just reduce that to one field and put what I have below:

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

CodePudding user response:

This worked for me:

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
  • Related