I would like to write a vba script for printing the content in column d and column e, which each row will (d1 and e1, d2 and e2, etc.) be printed on the same page as a single pdf file. The pdf file name is the corrsponding column a cell. I try to write the code below for the task. However, it fails and i would like to seek help from you guys
VBA Code:
Sub SaveToPDF()
Dim lastRow As Long
Dim fileName As String
Dim filePath As String
Dim ws As Worksheet
Dim pdf As Object
Dim i As Long
' Set the worksheet you want to save from
Set ws = ThisWorkbook.Worksheets("Sheet1")
' Determine the last row in the data set
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Loop through each row and save to PDF
For i = 1 To lastRow
' Set the PDF file name based on the value in column A
fileName = ws.Range("A" & i).Value
' Set the file path to save the PDF to
filePath = "C:\PDF Files\" & fileName & ".pdf" ' Change this to the directory you want to save to
' Save the data in columns D and E as a PDF
With ws.PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ws.Range("D" & i & ":E" & i).ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=filePath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next i
End Sub