you can also add code provided by ChatGPT.
before running the code, comment out your code on Work_BeforePrint().
copy and run this code:
' chatgpt
'
Sub DeterminePagesBeingPrinted()
Dim ws As Worksheet
Dim pageCount As Integer
Dim i As Integer
Dim rng As Range
' Set the worksheet and range to be printed
Set ws = ThisWorkbook.Sheets("Foglio1") ' Change "Sheet1" to your sheet name
Set rng = ws.UsedRange ' Or define a specific range
' Use the PageSetup to calculate total pages
With ws.PageSetup
.Zoom = False ' Ensure FitToPagesWide/High works
.FitToPagesWide = 1
.FitToPagesTall = False ' Set to False if you want height unrestricted
End With
' Calculate total pages
pageCount = ws.HPageBreaks.Count + 1 ' Horizontal pages
pageCount = pageCount * (ws.VPageBreaks.Count + 1) ' Multiply by vertical pages
' Output total pages
MsgBox "Total pages to print: " & pageCount
' Simulate printing and determine the pages being printed
For i = 1 To pageCount
' Print preview one page at a time
rng.PrintOut From:=i, To:=i, Preview:=True ' Change Preview to False to actually print
MsgBox "Currently printing page: " & i
Next i
End Sub