Microsoft KB Archive/242045

= How To Control Line Spacing When Printing From Visual Basic =

Article ID: 242045

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q242045



SUMMARY
The Printer object has no setting specifically for line spacing, but you can control the line spacing with the CurrentY property of the Printer object.



MORE INFORMATION
The print position on a page is determined by two properties of the Printer object, CurrentX and CurrentY, which control the X and Y coordinates respectively. Each time you call Printer.Print, the CurrentY position is incremented to advance to the next line. The amount of the increment is determined by the font and printer driver. So, to override the default line spacing you need to manipulate Printer.CurrentY directly.

By default, this property uses twips and there are 1440 twips per logical inch. So for six lines per inch, you need each line to take 240 twips, and for eight lines per inch, you need 180 twips. You can set Printer.ScaleMode to use a different predefined scale or set the ScaleHeight and ScaleWidth properties to create a custom scale. It might also be useful to employ the TextHeight property to determine the minimum height for your text, and TextWidth to determine the width of your string. Please check Online Help and see the REFERENCES section below for more information on using these properties.

The code below demonstrates the effect of changing the CurrentY property between lines. Note that you can cause lines to overlap without generating an error.

Step-by-Step Example
 Start a new Standard EXE project. Form1 is created by default.  Add the following code to the code window of Form1: Option Explicit

Private Sub Form_Click Dim Lines As Integer, Y As Long, OutStr As String

With Printer .Print "Font is " & .Font.Name & " " & .Font.Size & " pt" .CurrentY = .CurrentY + 500  ' Skip some space .Print "First, print normally ..." .Print "Line 1 " & ".CurrentY = " & .CurrentY .Print "Line 2 " & ".CurrentY = " & .CurrentY .Print "Line 3 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY + 500  ' Skip some space .Print "Now shorten the lines for less line spacing ..." .Print "Line 1 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY - 50 .Print "Line 2 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY - 50 .Print "Line 3 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY + 500  ' Skip some space .Print "Now increase line spacing ..." .CurrentY = .CurrentY + 50 .Print "Line 1 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY + 50 .Print "Line 2 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY + 50 .Print "Line 3 " & ".CurrentY = " & .CurrentY .CurrentY = .CurrentY + 500  ' Skip some space .Print "Now specify 6 lines per inch ..." ' Start at an even position to better see increments. .CurrentY = .CurrentY + (1000 - .CurrentY Mod 1000) For Lines = 1 To 6  ' These 6 lines take 1 inch on the page. Y = .CurrentY  ' Remember current Y position. .Print "Line " & Lines & ": " & "6 Lines per inch - " & _ ".CurrentY = " & .CurrentY ' Increment the Y position by 1/6th of an inch (240 twips). .CurrentY = Y + 240  ' Reset to previous position + 240. Next Lines .EndDoc  ' End the print job. End With End Sub

Private Sub Form_Load ' Select a standard Font and size (Optional) Printer.Font.Name = "Arial" Printer.Font.Size = 8 End Sub  Run the project and click on the form. The resulting printed page demonstrates different line spacings.

