Создание нескольких файлов PDF из формы - PullRequest
0 голосов
/ 10 июля 2020

Мне нужна помощь с приведенным ниже кодом, я получил этот код в учебнике, и он отлично работает для меня, суть этого вопроса в том, что приведенный ниже код генерирует только 1 PDF-файл из постоянной PDF-формы с использованием ячеек «А2» и «В2». Как я могу сделать L oop, чтобы код генерировал 1 файл PDF на основе каждой строки из листа с текстом?

Я предоставляю код ниже. Буду очень признателен за помощь и время.

Option Explicit

Sub Write_to_pdf_form()

'declaring variables
Dim pdfApp As Acrobat.AcroApp
Dim pdfDoc As Acrobat.AcroAVDoc
Dim Support_doc As Acrobat.AcroPDDoc
Dim pdffile
Dim wsDocs As Worksheet
Dim outputname


'declaring output path
pdffile = "C:\Users\User\Documents\testesbulkpdf\Forms.pdf"
Dim pdf_form As AFORMAUTLib.AFormApp


'declaring fields
Dim num_doc As AFORMAUTLib.Field
Dim desc_doc As AFORMAUTLib.Field

Set pdfApp = CreateObject("AcroExch.App")
Set pdfDoc = CreateObject("AcroExch.AVDoc")

If pdfDoc.Open(pdffile, "") = True Then
pdfDoc.BringToFront
pdfApp.Show


'setting fields names
Set pdf_form = CreateObject("AFORMAUT.App")
Set num_doc = pdf_form.Fields("N")
Set desc_doc = pdf_form.Fields("descrição documento")

'setting fields values
num_doc.Value = Worksheets("docs").Range("A2").Value

desc_doc.Value = Worksheets("docs").Range("B2").Value


'setting output name of PDF
outputname = "Doc." & num_doc.Value & "-" & desc_doc.Value



Set Support_doc = pdfDoc.GetPDDoc


If Support_doc.Save(PDSaveFull, "C:\Users\User\Documents\testesbulkpdf\" & outputname & ".pdf") Then

Debug.Print "Saved"
Else
Debug.Print "Failed to save the doc"

End If

pdfDoc.Close True
Support_doc.Close
pdfApp.Exit
Set num_doc = Nothing
Set desc_doc = Nothing

Set pdfDoc = Nothing
Set Support_doc = Nothing
Set pdfApp = Nothing



End If



End Sub

1 Ответ

0 голосов
/ 17 июля 2020

Я получил это, используя For Each и несколько лейблов, у меня все отлично работает.

For Each cell In Worksheets("docs").Range("A2:B500")


If cell.Value = "" Then
GoTo Line4
End If

    If cell.Column = 1 Then
    cell.Activate
        If ActiveCell.Value <> "" Then
        num_doc.Value = ActiveCell.Value
        GoTo Line1
        End If
    End If

    
If cell.Column = 2 Then
    cell.Activate
        If ActiveCell.Value = "" Then
        GoTo Line1
        Else: desc_doc.Value = ActiveCell.Value
            If desc_doc.Value <> "" And num_doc.Value <> "" Then
            GoTo Line3
            End If
        End If
End If
    

 If desc_doc Or num_doc = "" Then
 GoTo Line4
 End If


  
    
                         
           
 Line3:

 outputname = "Doc." & num_doc.Value & "-" & desc_doc.Value


 Set Support_doc = pdfDoc.GetPDDoc

 If Support_doc.Save(PDSaveFull, "C:\Users\User\Documents\testesbulkpdf\" & outputname 
 & ".pdf") Then
 Debug.Print "Saved"
Else
Debug.Print "Failed to save the doc"
End If
    
Line1:
Next cell


Line4:

pdfDoc.Close True
Support_doc.Close
pdfApp.Exit
Set num_doc = Nothing
Set desc_doc = Nothing

Set pdfDoc = Nothing
Set Support_doc = Nothing
Set pdfApp = Nothing

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...