Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub write_to_pdf_form()
- 'PDF Settings and Fields Control
- Dim pdfApp As Acrobat.AcroApp
- Dim pdfDoc As Acrobat.AcroAVDoc
- Dim Support_doc As Acrobat.AcroPDDoc
- Dim pdf_form As AFORMAUTLib.AFormApp
- Dim pdf_form_flds As AFORMAUTLib.Fields
- Dim resp As String
- Dim FormX As AFORMAUTLib.Field ' This is the text Field name in the PDF being written to
- Dim opt_bt1 As AFORMAUTLib.Field ' This is the button Field name in the PDF being written to
- 'File Path Control
- Dim exportPath As String
- Dim exportFileNameStaging As String
- Dim exportFileName As String
- Dim saveFolder As String
- Dim pdf_form_file As String
- Dim objFSO
- Set objFSO = CreateObject("scripting.filesystemobject")
- 'Macro and Cell Reference Control
- Dim selectedRows As Integer
- Dim selectedCol As String
- Dim workingRow As Integer
- Dim currentField As String
- Dim currentValue As Variant
- Dim headerReference As String
- Dim headerValues As Variant ' Hardcoded Field Names to map to PDF Template Fields
- headerValues = Array("COMPANY", "PLANT", "UNIT NO", "TAG NUMBER", "LOOP NUMBER", "DRAWING NUMBER", "SERIAL NUMBER", "MANUFACTURER") ' Field names from the PDF
- 'Row Validation
- selectedRows = Selection.Row + Selection.Rows.Count - 1
- resp = MsgBox("Generate report for selected rows " & Selection.Row & " through " & selectedRows & "?", vbYesNo)
- If resp = vbNo Then
- End
- End If
- 'Header Validation
- headerReference = InputBox("Enter row number of your column headers." & vbNewLine & vbNewLine & "Ex: enter 5 if your Column Names are in row 5.", "Column Header Validation", 1)
- If StrPtr(headerReference) = 0 Then
- MsgBox "Cancelled."
- End
- End If
- If CStr(headerReference) <= 0 Then
- MsgBox "Error. Bad Value."
- End
- End If
- 'Set var pdf_form_file (the PDF file which is the template to be populated)
- MsgBox "Select your PDF template file"
- pdf_form_file = Application.GetOpenFilename(FileFilter:="Adobe PDF Files (*.pdf*), *.pdf*", Title:="Choose a PDF template file", MultiSelect:=False)
- If CStr(pdf_form_file) = "False" Then
- MsgBox "Report generator cancelled."
- End
- End If
- resp = MsgBox("Your template filepath is: " & vbNewLine & vbNewLine & pdf_form_file & vbNewLine & vbNewLine & "Is this correct?", vbYesNo)
- If resp = vbNo Then
- MsgBox "Report generator cancelled."
- End
- End If
- 'Set save path of generated PDF forms
- saveFolder = "output"
- mkdir (saveFolder)
- exportPath = Application.ActiveWorkbook.path & "\" & saveFolder & "\"
- 'MsgBox "Your reports will be saved to: " & vbNewLine & vbNewLine & exportPath
- Dim x As Integer ' Iterator between rows
- Dim i As Integer ' Iterator within a row
- 'Iterate through rows
- For x = Selection.Row To selectedRows:
- Set pdfApp = CreateObject("AcroExch.App")
- Set pdfDoc = CreateObject("AcroExch.AVDoc")
- If pdfDoc.Open(pdf_form_file, "") = True Then
- pdfDoc.BringToFront
- pdfApp.Show
- Set pdf_form = CreateObject("AFORMAUT.App")
- 'Iterate through cells within row
- workingRow = x
- For i = 1 To 20
- selectedCol = Col_Letter(i)
- 'If IsInArray(headerValues, Cells(1, selectedCol).Value) Then
- If IsInArray2(headerValues, Cells(headerReference, selectedCol).Value) Then
- 'MsgBox "The value of header value is " & Cells(1, selectedCol).Value
- currentField = Cells(headerReference, selectedCol).Value
- 'MsgBox "The value of currentField is " & currentField
- 'MsgBox "The value of workingRow is " & workingRow
- currentValue = Cells(workingRow, selectedCol).Value
- 'MsgBox "The value of currentValue is " & currentValue
- Set FormX = pdf_form.Fields(currentField)
- FormX.Value = currentValue
- currentField = ""
- currentValue = ""
- End If
- selectedCol = ""
- Next i
- Set Support_doc = pdfDoc.GetPDDoc
- exportFileNameStaging = objFSO.GetBaseName(pdf_form_file)
- exportFileName = exportFileNameStaging & "_" & Format(Now(), "yyyy-mm-dd") & "_idx_" & x & ".pdf"
- If Support_doc.Save(PDSaveFull, exportPath & exportFileName) Then
- Else
- MsgBox "Error. Failed to Save" & exportFileName & ". Check if the file is open."
- End If
- pdfDoc.Close True
- Support_doc.Close
- End If
- Next x
- 'Close Adobe and clear memory.
- pdfApp.Exit
- Set FormX = Nothing
- Set pdfDoc = Nothing
- Set Support_doc = Nothing
- 'Open folder of exports
- Shell "C:\WINDOWS\explorer.exe """ & exportPath & "", vbNormalFocus
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement