Here is the complete LibreOffice Basic macro code, incorporating all the requested features:
Exports as PDF.
Exports only the defined Print Area (
A1:I37).Uses the sheet
"Report card t-1".Names the PDF file based on the value in cell B7.
Loops through roll numbers 1 to 40.
You need to copy and paste this entire code block into your LibreOffice Basic IDE module.
Complete PDF Export Macro
Basic
Sub ExportAllReportsToPDF_FinalFix()
' --- CONFIGURATION ---
Const START_ROLL As Integer = 1
Const END_ROLL As Integer = 40
' 1. The name of the sheet that contains the report card
Const SHEET_NAME As String = "Report card t-1"
' 2. The named range for the cell that holds the roll number (used for VLOOKUP)
Const ROLL_CELL_NAME As String = "RollNoCell"
' 3. The EXACT cell range of your report card print area
Const PRINT_AREA As String = "A1:I37"
' 4. Set your desired output folder path here. MUST exist!
Dim sOutputFolder As String
sOutputFolder = "C:\StudentReports\" ' <-- CHANGE THIS FOLDER PATH
' 5. The cell containing the desired filename value (B7)
Const FILENAME_CELL As String = "B7"
' ---------------------
Dim oDoc As Object
oDoc = ThisComponent
' --- Object Retrieval ---
Dim oSheet As Object
On Error GoTo ErrorHandlerSheet
oSheet = oDoc.Sheets.getByName(SHEET_NAME)
Dim oRollNoCell As Object
On Error GoTo ErrorHandlerCell
oRollNoCell = oSheet.getCellRangeByName(ROLL_CELL_NAME)
Dim oPrintRange As Object
On Error GoTo ErrorHandlerRange
oPrintRange = oSheet.getCellRangeByName(PRINT_AREA)
Dim oFilenameCell As Object
oFilenameCell = oSheet.getCellRangeByName(FILENAME_CELL)
' Convert the path to URL format (critical for storeToURL)
Dim sOutputDirURL As String
sOutputDirURL = ConvertToURL(sOutputFolder)
' --- PDF Export Properties ---
Dim args(1) as New com.sun.star.beans.PropertyValue
args(0).Name = "FilterName"
args(0).Value = "calc_pdf_Export"
' CRITICAL: Tell the filter to export only the "Selection" (the cells we select in the loop)
args(1).Name = "FilterData"
args(1).Value = Array(Array("Selection", 0, Array()))
' ------------------------------
Dim i As Integer
For i = START_ROLL To END_ROLL
' 1. Update the Roll Number cell
oRollNoCell.Value = i
' Wait briefly for VLOOKUP to update
Wait 500
' 2. CRITICAL STEP: Select the specific print range on the sheet
oDoc.CurrentController.setActiveSheet(oSheet)
oDoc.CurrentController.select(oPrintRange)
' 3. Define the output file name using the value from B7
Dim sFileNameValue As String
Dim sFileName As String
' Get the string value from B7
sFileNameValue = oFilenameCell.getString()
' Clean the filename value and add the Roll No for uniqueness
sFileName = CleanFileName(sFileNameValue) & "_Roll_" & i & ".pdf"
' 4. Combine directory and filename
Dim sFullFileName As String
sFullFileName = sOutputDirURL & sFileName
' 5. Store/Export to PDF
On Error GoTo ErrorHandlerStore
oDoc.storeToURL(sFullFileName, args())
Next i
' Optional: Clear the selection when done
oDoc.CurrentController.select(oRollNoCell)
MsgBox "Batch PDF export complete! 40 files saved to: " & sOutputFolder, 64, "Export Status"
Exit Sub
' --- ERROR HANDLING ---
ErrorHandlerStore:
MsgBox "An error occurred during export for Roll No: " & i & vbCrLf & _
"Check if the folder '" & sOutputFolder & "' exists and you have write permissions." & vbCrLf & _
"Error Description: " & Error(), 16, "Export Error!"
Resume Next
ErrorHandlerRange:
MsgBox "Error: The Print Area Range '" & PRINT_AREA & "' is invalid or not found on the sheet.", 16, "Setup Error!"
Exit Sub
ErrorHandlerCell:
MsgBox "Error: Roll Number Cell Name '" & ROLL_CELL_NAME & "' not found on the sheet.", 16, "Setup Error!"
Exit Sub
ErrorHandlerSheet:
MsgBox "Error: Sheet Name '" & SHEET_NAME & "' not found in the document.", 16, "Setup Error!"
Exit Sub
End Sub
' ----------------------------------------------------
' Helper function to clean illegal filename characters (MUST be outside the main Sub)
' ----------------------------------------------------
Function CleanFileName(sInput As String) As String
Dim sOutput As String
sOutput = sInput
' Replace common illegal characters with an underscore
sOutput = Replace(sOutput, "\", "_")
sOutput = Replace(sOutput, "/", "_")
sOutput = Replace(sOutput, ":", "_")
sOutput = Replace(sOutput, "*", "_")
sOutput = Replace(sOutput, "?", "_")
sOutput = Replace(sOutput, """", "_")
sOutput = Replace(sOutput, "<", "_")
sOutput = Replace(sOutput, ">", "_")
sOutput = Replace(sOutput, "|", "_")
' Trim extra spaces
sOutput = Trim(sOutput)
CleanFileName = sOutput
End Function
Important Steps Before Running
Update Configuration: Double-check the four
Constvalues at the top of the macro match your file's setup.Define RollNoCell: Ensure the roll number input cell is named
RollNoCellin the Name Box.Create Folder: Manually create the folder specified in
sOutputFolder(e.g.,C:\StudentReports\).Run: Go to Tools Macros Run Macro (or use the green triangle button in the IDE).
2. Use a loop to extract page 128 from each PDF
Linux/macOS (Bash):
This will create a folder extracted_pages with files like file1_p128.pdf, file2_p128.pdf, etc.