The code below takes a table in excel and inputs variables from columns into textboxes in a pdf template. This is done by using the sendkeys function over and over. I was wondering if there was an easy way to make this a looping function over the sendkeys section of code since I would like to be able to add many more columns/variables if necessary without having to copy and paste this code a lot.
This is all done after a button is clicked on the sheet and will run through the table while saving each created pdf to a new folder. The only part I would like to change is the long list of sendkeys to a looping function.
Thank you for any/all help!
Sub CreatePDFForms()
Dim PDFTemplateFile, NewPDFName, SavePDFFldr, Desc As String
Dim custRow, LastRow As Long
With Sheet1
LastRow = .Range("A999").End(xlUp).Row 'Last Row (just set it lower than the last data row)
PDFTemplateFile = .Range("F2").Value 'Template File Name, needs to be the same as the set cell above
SavePDFFldr = .Range("F4").Value 'Save PDF Folder, needs to be the same as well
ThisWorkbook.FollowHyperlink PDFTemplateFile
Application.Wait Now + 0.000004
'CHANGE THE "LastRow" TO THE SAME NUMBER AS FIRST ROW TO TEST IF NEEDED
For CustRow = 13 To 13 ' LastRow
D1 = .Range("L" & CustRow).Value 'DEFINING THE VARIABLES AS "D##" WITH
D2 = .Range("B" & CustRow).Value 'REFERENCE TO SPECIFIC COLUMNS
D3 = .Range("AC" & CustRow).Value
D4 = .Range("C" & CustRow).Value
D5 = .Range("Y" & CustRow).Value
D6 = .Range("AB" & CustRow).Value
D7 = .Range("Z" & CustRow).Value
D8 = .Range("U" & CustRow).Value
'D9 = .Range("AA" & CustRow).Value
'D10 = .Range("AA" & CustRow).Value
Description = D4 ' CHANGE THE D## IN THIS LINE TO THE DESCRIPTION VARIABLE FOR FILE NAME CREATION
' CHANGE THE "AA" TO THE ROW ASSOCIATED WITH THAT VARIABLE
' GET RID OF APOSTROPHE TO RELEASE FROM COMMENT LAYER
Application.SendKeys "{Tab}", True
Application.SendKeys D1, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D2, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D3, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D4, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D5, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D6, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D7, True
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys D8, True
Application.Wait Now + 0.00001
'Application.SendKeys "{Tab}", True
'Application.SendKeys D##, True
'Application.Wait Now + 0.00001
'Application.SendKeys "{Tab}", True
'Application.SendKeys D##, True
'Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys "{Esc}", True
Application.SendKeys "^(p)", True ' opens the print menu
Application.Wait Now + 0.00001
Application.SendKeys "{Tab}", True
Application.SendKeys "{Enter}", True
Application.Wait Now + 0.00001
Application.SendKeys "{l}", True ' change to a landscape orientation
Application.SendKeys "{Enter}", True
Application.Wait Now + 0.00001
Application.SendKeys "{Left}", True
Application.SendKeys "{Enter}", True
' *********************** BE SURE THAT PRINT TO PDF IS DEFAULT *************************************
Application.SendKeys "{Enter}", True
Application.Wait Now + 0.00001
'CHANGE THE D## IN THE BELOW LINES TO CHANGE THE NAME OF THE FILE
If Dir(SavePDFFldr & "\" & Description & ".pdf") <> Empty Then Kill (SavePDFFldr & "\" & Description & ".pdf")
' THE ABOVE CODE DELETES A FILE WITH THE SAME NAME IN THE FOLDER
' IF YOU WANT TO KEEP OLD COPIES, SAVE TO A DIFFERENT FOLDER OR MOVE THE OLDER DRAFTS
Application.SendKeys SavePDFFldr & "\" & Description & ".pdf"
Application.Wait Now + 0.00001
Application.SendKeys "%(s)"
Application.Wait Now + 0.00001
Next custRow
' THE FOLLOWING CODE CLOSES THE PROGRAM AND FOLDERS
Application.SendKeys "^(q)", True
Application.SendKeys "{numlock}%s", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Enter}", True
End With
End Sub