Advertisement
Guest User

Untitled

a guest
Dec 7th, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub write_to_pdf_form()
  2.  
  3. 'PDF Settings and Fields Control
  4. Dim pdfApp As Acrobat.AcroApp
  5. Dim pdfDoc As Acrobat.AcroAVDoc
  6. Dim Support_doc As Acrobat.AcroPDDoc
  7. Dim pdf_form As AFORMAUTLib.AFormApp
  8. Dim pdf_form_flds As AFORMAUTLib.Fields
  9. Dim resp As String
  10. Dim FormX As AFORMAUTLib.Field   ' This is the text   Field name in the PDF being written to
  11. Dim opt_bt1 As AFORMAUTLib.Field ' This is the button Field name in the PDF being written to
  12.  
  13. 'File Path Control
  14. Dim exportPath As String
  15. Dim exportFileNameStaging As String
  16. Dim exportFileName As String
  17. Dim saveFolder As String
  18. Dim pdf_form_file As String
  19. Dim objFSO
  20. Set objFSO = CreateObject("scripting.filesystemobject")
  21.  
  22. 'Macro and Cell Reference Control
  23. Dim selectedRows As Integer
  24. Dim selectedCol As String
  25. Dim workingRow As Integer
  26. Dim currentField As String
  27. Dim currentValue As Variant
  28. Dim headerReference As String
  29. Dim headerValues As Variant ' Hardcoded Field Names to map to PDF Template Fields
  30. headerValues = Array("COMPANY", "PLANT", "UNIT NO", "TAG NUMBER", "LOOP NUMBER", "DRAWING NUMBER", "SERIAL NUMBER", "MANUFACTURER") ' Field names from the PDF
  31.  
  32. 'Row Validation
  33. selectedRows = Selection.Row + Selection.Rows.Count - 1
  34. resp = MsgBox("Generate report for selected rows " & Selection.Row & " through " & selectedRows & "?", vbYesNo)
  35. If resp = vbNo Then
  36.     End
  37. End If
  38.  
  39. 'Header Validation
  40. 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)
  41. If StrPtr(headerReference) = 0 Then
  42.     MsgBox "Cancelled."
  43.     End
  44. End If
  45. If CStr(headerReference) <= 0 Then
  46.     MsgBox "Error. Bad Value."
  47.     End
  48. End If
  49.  
  50. 'Set var pdf_form_file (the PDF file which is the template to be populated)
  51. MsgBox "Select your PDF template file"
  52. pdf_form_file = Application.GetOpenFilename(FileFilter:="Adobe PDF Files (*.pdf*), *.pdf*", Title:="Choose a PDF template file", MultiSelect:=False)
  53. If CStr(pdf_form_file) = "False" Then
  54.     MsgBox "Report generator cancelled."
  55.     End
  56. End If
  57. resp = MsgBox("Your template filepath is: " & vbNewLine & vbNewLine & pdf_form_file & vbNewLine & vbNewLine & "Is this correct?", vbYesNo)
  58. If resp = vbNo Then
  59.     MsgBox "Report generator cancelled."
  60.     End
  61. End If
  62.  
  63. 'Set save path of generated PDF forms
  64. saveFolder = "output"
  65. mkdir (saveFolder)
  66. exportPath = Application.ActiveWorkbook.path & "\" & saveFolder & "\"
  67. 'MsgBox "Your reports will be saved to: " & vbNewLine & vbNewLine & exportPath
  68.  
  69. Dim x As Integer ' Iterator between rows
  70. Dim i As Integer ' Iterator within a row
  71.  
  72. 'Iterate through rows
  73. For x = Selection.Row To selectedRows:
  74.    
  75.     Set pdfApp = CreateObject("AcroExch.App")
  76.     Set pdfDoc = CreateObject("AcroExch.AVDoc")
  77.     If pdfDoc.Open(pdf_form_file, "") = True Then
  78.        
  79.         pdfDoc.BringToFront
  80.         pdfApp.Show
  81.        
  82.         Set pdf_form = CreateObject("AFORMAUT.App")
  83.        
  84.         'Iterate through cells within row
  85.        workingRow = x
  86.         For i = 1 To 20
  87.        
  88.         selectedCol = Col_Letter(i)
  89.        
  90.             'If IsInArray(headerValues, Cells(1, selectedCol).Value) Then
  91.            If IsInArray2(headerValues, Cells(headerReference, selectedCol).Value) Then
  92.             'MsgBox "The value of header value is " & Cells(1, selectedCol).Value
  93.                
  94.                     currentField = Cells(headerReference, selectedCol).Value
  95.                     'MsgBox "The value of currentField is " & currentField
  96.                    
  97.                     'MsgBox "The value of workingRow is " & workingRow
  98.                    currentValue = Cells(workingRow, selectedCol).Value
  99.                     'MsgBox "The value of currentValue is " & currentValue
  100.                    
  101.                     Set FormX = pdf_form.Fields(currentField)
  102.                     FormX.Value = currentValue
  103.                
  104.                     currentField = ""
  105.                     currentValue = ""
  106.            
  107.             End If
  108.            
  109.         selectedCol = ""
  110.            
  111.         Next i
  112.    
  113.         Set Support_doc = pdfDoc.GetPDDoc
  114.        
  115.         exportFileNameStaging = objFSO.GetBaseName(pdf_form_file)
  116.         exportFileName = exportFileNameStaging & "_" & Format(Now(), "yyyy-mm-dd") & "_idx_" & x & ".pdf"
  117.        
  118.         If Support_doc.Save(PDSaveFull, exportPath & exportFileName) Then
  119.         Else
  120.             MsgBox "Error. Failed to Save" & exportFileName & ". Check if the file is open."
  121.         End If
  122.        
  123.         pdfDoc.Close True
  124.         Support_doc.Close
  125.    
  126.     End If
  127.  
  128. Next x
  129.  
  130. 'Close Adobe and clear memory.
  131. pdfApp.Exit
  132. Set FormX = Nothing
  133. Set pdfDoc = Nothing
  134. Set Support_doc = Nothing
  135.  
  136. 'Open folder of exports
  137. Shell "C:\WINDOWS\explorer.exe """ & exportPath & "", vbNormalFocus
  138.  
  139. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement