将数据从excel复制到pdf表单,适用于第一个但是

时间:2014-07-18 19:28:09

标签: excel vba pdf acrobat

我想使用vba将数据从Excel导出到pdf-Form。 我用这种方法: https://forums.adobe.com/thread/302309

当我只复制一个字段时,它可以工作,但我想复制A1:K2中的所有字段,其中字段标题始终位于顶部,内容位于下面的行中。 我认为我的问题是,当我尝试复制下一个值和字段标题时,我不会切换回Excel。但我不知道如何正确地做到这一点。 如果有人能告诉我,我会很高兴。

这些文件可以在这里下载: http://www.xn--frank-mller-zhb.net/Formulardings.zip

Sub Pdfdings()


Dim gApp As Acrobat.CAcroApp
Dim avdoc As Acrobat.CAcroAVDoc
Dim gPDDoc As Acrobat.CAcroPDDoc
Const DOC_FOLDER As String = "C:\Users\Frank\Documents"
Dim x As Boolean

Set gApp = CreateObject("AcroExch.App")
Set gPDDoc = CreateObject("AcroExch.PDDoc")
Set avdoc = CreateObject("AcroExch.AVDoc")

'Hides Acrobat - So Far So Good
'gApp.Hide

Dim FormApp As AFORMAUTLib.AFormApp
Dim AcroForm As AFORMAUTLib.Fields
Dim Field As AFORMAUTLib.Field
Dim z, i, j, n As Integer
Dim wksTab1 As Worksheet
Dim Feld, Inhalt As String

Set wksTab1 = Sheets("Tabelle2")

'Open PDF that I choose. Acrobat still has not flashed on my screen
j = 1
i = 2
While i < 3
    x = avdoc.Open(DOC_FOLDER & "\formular_ve01a.pdf", "temp")

    'Acrobat Now Pops up on my screen. However, I get an error without this line. avdoc.Show works the same as Maximize it seems.
    avdoc.Maximize (1)

    'Hides it again, right after it opens. This creates a flash
    'gApp.Hide

    Set FormApp = CreateObject("AFormAut.App")

    While j < 39



        'If the Maximize line is not there, this is where I receive error about document viewer
        Feld = wksTab1.Cells(1, j).Value
        Inhalt = wksTab1.Cells(i, j).Value

        For Each Field In FormApp.Fields
            If Field.Name = Feld Then
                Field.Value = Inhalt
            End If
        Next

        j = j + 1
        Wend
    Dim sDoc
    Set sDoc = avdoc.GetPDDoc
    saveOk = sDoc.Save(1, DOC_FOLDER & "\OK_Formular" & wksTab1.Cells(1, 1).Value & ".pdf")
    avdoc.Close (1)
    gApp.Exit
    i = i + 1
Wend
End Sub

2 个答案:

答案 0 :(得分:0)

  1. 将A1:K2设为打印范围
  2. 将您的打印机设置为PDF Writer(CutePDF或PDF995或其他)
  3. 打印

答案 1 :(得分:0)

我在另一个论坛的帮助下得到的解决方案

      <pre>While j < 39
         'If the Maximize line is not there, this is where I receive error about document viewer
         Feld = wksTab1.Cells(1, j).Value
         Inhalt = wksTab1.Cells(i, j).Value
         FormApp.Fields(Feld).Value = Inhalt
         j = j + 1
      Wend

谢谢大家!