使用 VBA 写入并保存为 PDF

发布于 2025-01-10 00:48:22 字数 1224 浏览 0 评论 0原文

我已经能够成功写入 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2025-01-17 00:48:22

这对我有用:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文