I have an excel file and I want to merge the data to one pdf templates and make many different copies of PDF (Save as new PDF files to a folder).
I take reference from a youtube turotial:
https://www.youtube.com/watch?v=uU55FCbPHCI
I have done some parts but the problem is I don’t want to print the pdf.
However, the process in the Youtube tutorial involve printing pdf.
After I delete some lines, I think I mess up the marco code.
Can someone please kindly help me to fix the code? Thank you very much.
The code is as below:
Sub PDFTemplate()
Dim PDFFIdr As FileDialog
Set PDFFldr = Application.FileDialog(msoFileDialogFilePicker)
With PDFFldr
.Title = "Select PDF files to attach"
.Filters.Add "PDF Types Files", "*.pdf", 1
If .Show <> -1 Then GoTo NoSelection
Worksheets("Sheet1").Range("B11").Value = .SelectedItems(1)
End With
NoSelection:
End Sub
Sub SavePDFFolder()
Dim PDFFIdr As FileDialog
Set PDFFldr = Application.FileDialog(msoFileDialogFolderPicker)
With PDFFldr
.Title = "Select a Folder"
If .Show <> -1 Then GoTo NoSelection:
Worksheets("Sheet1").Range("B12").Value = .SelectedItems(1)
End With
NoSelection:
End Sub
Sub CreatePDFForms()
Dim PDFTemplate As String
Dim NewPDFName As String
Dim SavePDFFolder As String
Dim ReceiptNo As String
Dim PaidDate As Date
Dim CustRow As Long
Dim LastRow As Long
With Worksheets("Sheet1")
LastRow = .Range("A9999").End(xlUp).Row ' Find the last row in column A
PDFTemplateFile = .Range("B11").Value
SavePDFFolder = .Range("B12").Value
If PDFTemplateFile = "" Or SavePDFFolder = "" Then
MsgBox "Please select both a PDF template and a save folder.", vbExclamation
Exit Sub
End If
' Open the PDF template file maximized
Shell "explorer.exe """ & PDFTemplateFile & """", vbMaximizedFocus
' Wait for a short time to ensure the PDF opens (adjust as needed)
Application.Wait Now + TimeValue("00:00:03")
For CustRow = 2 To 2
ReceiptNo = .Range("A" & CustRow).Value
PaidDate = .Range("C" & CustRow).Value
ReceivedFrom = .Range("B" & CustRow).Value
' Send data to the PDF form
Application.SendKeys "{Tab}", True
Application.SendKeys ReceiptNo, True
Application.Wait Now + TimeValue("00:00:01")
Application.SendKeys "{Tab}", True
Application.SendKeys PaidDate, True
Application.Wait Now + TimeValue("00:00:01")
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys ReceivedFrom, True
Application.Wait Now + TimeValue("00:00:01")
' Save As dialog to save the PDF in the specified folder with a new name
Application.SendKeys "^+s", True ' Ctrl+Shift+S to open Save As dialog
Application.Wait Now + TimeValue("00:00:02")
FileName = SavePDFFolder & "" & ReceiptNo & " " & PaidDate & ".pdf"
Application.SendKeys FileName, True
Application.Wait Now + TimeValue("00:00:02")
Application.SendKeys "{ENTER}", True
Application.Wait Now + TimeValue("00:00:02")
Next CustRow
End With
End Sub