Re: Best fit print area
OK, I've realized this thread has gotten way off topic from where I started initially. The code I have now to do the conditional printing is as follows, and was provided here by user royUK.
Following is a modified version of his code:
'---------------------------------------------------------------------------------------
' Module : ThisWorkbook
' DateTime : 19/10/2005 19:11
' Author : Roy Cox, edited to suit specific needs of this work book by Simon Silk
' Website : www.excel-it.com
' Purpose : stop print to run code. Then resume print
'---------------------------------------------------------------------------------------
Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Application.EnableEvents = False
Cancel = True 'stop print
Dim response As String
response = MsgBox("Print just this sheet?" & Chr(10) & "Yes = print just current sheet" & Chr(10) & "No = Print entire workbook" & Chr(10) & _
"Click Cancel to cancel printing.", vbYesNoCancel, "Print What?")
' If only printing current page:
If response = vbYes Then
' If printing Project Review:
If (ActiveSheet.Name = "Project Review") Then
Call unhighlightUserEntryCells
Call FitPages
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Call highlightUserEntryCells
Else
' If printing any other sheet:
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End If
' If printing entire workbook:
ElseIf response = vbNo Then
Call unhighlightUserEntryCells
Call FitPages
ActiveWorkbook.PrintOut Copies:=1, Collate:=True
Call highlightUserEntryCells
End If
Application.EnableEvents = True
End Sub
Display More
This works fine for my needs, I'm just posting it so others can see.
Now I want to return to my original question about formatting for printing.
The following is the code I have, which is supposed to give a best-fit number of pages for the height of the document. However, it is unpredictable. Sometimes it will make good calls, like three pages or so for a document that realistically is around 3 pages long. Right now however, it attempts to cram about a 5-pager onto one page. Can anyone see a problem with this code?
Some of the individual rows in my sheet grow in height as the user fills in info, but I don't think that explains the problem here.
Sub FitPages()
Worksheets("Project Review").Activate
Dim nRows As Integer, s As Double, i As Integer
nRows = 117 ' Sheet currently only extends to row 117. Modify this as sheet is modified.
s = 0
For i = 1 To nRows
s = s + Rows(i).Width
Next i
s = s / 72 'total inches long
i = Application.Round(s / 10, 0) 'number of pages long To Do
ActiveSheet.PageSetup.PrintArea = "$A$1:$I$117"
With ActiveSheet.PageSetup
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = i
End With
End Sub
Display More