Visual Basic 6 printing


I am using the printer. command in VB6 to output data to a template.

I have a text box set as a multiline text box and I can input the name, address etc on separate lines but when it comes to printing I have set the printer.currentx and printer.currenty to the coordinates on the template, the first line printed is in the correct place but all other lines are pushed back to the edge of the page.

How can I format the text to be at the same column as to print to the template?

Any help would be appreciated


Who is Participating?
inthedarkConnect With a Mentor Commented:
If you are new to the printer here is a load of stuff wihci you need to know...

1) How to do what you want and print a multi line text.

2) How to justify to left/right or centre.

3) How to test the print on a preview form.

4) how to set a zoom percent.

5) How to print some multiline text on the left then some in the middle still at the top of the print area then move done to lowest point to continue printing.

Hope this helps.

To do what you want

Paste the following code into a blank form then run:
Option Explicit

' set the scale units needed
Const ScaleUnits = "M" ' M=metric or I=Inches
Dim TWIPS As Single
Dim LineGapFactor As Single ' to hold the LINEGAP between lines

' margins see SetupScaleUnits
Dim LeftMargin As Single
Dim RightMargin As Single
Dim CentreMargin As Single
Dim TopMargin As Single

Dim ZoomPercent As Single
Dim ZoomFactor As Single
Dim CurrentFont As Single

Dim PrOut As Object

Public Sub PrintMoveDown()

Dim SaveFontSize As Single
SaveFontSize = PrOut.Font.Size

PrOut.Font.Size = CurrentFont

' move down to next print line
PrOut.CurrentY = PrOut.CurrentY + PrOut.TextHeight("X") * LineGapFactor
PrOut.Font.Size = SaveFontSize

End Sub

Sub PrintLines(pPrintXPos As Single, psData As String, Optional pJustify As String = "L", Optional pFontSize As Single = 0)

Dim SaveFontSize As Single
SaveFontSize = PrOut.Font.Size

Dim FontSize As Single
FontSize = pFontSize * ZoomFactor
Dim bSetFont As Boolean

'do they wish to specify font size

If FontSize = 0 Then
    FontSize = SaveFontSize * ZoomFactor
End If

Static LastFont As Single
Static LastZoomPercent As Single

If LastFont <> FontSize Or ZoomPercent <> LastZoomPercent Then
    '   set font size

    PrOut.Font.Size = FontSize * ZoomFactor
    CurrentFont = FontSize
    LastFont = FontSize
    LastZoomPercent = ZoomPercent

End If

Dim sLines() As String

' Split the text into lines
sLines = Split(psData, vbCrLf)

Dim TabPos As Single

Dim lc As Long

Dim sFontSize As Single

' create a left margin
TabPos = pPrintXPos * ZoomFactor

Dim TextLeftPos As Single

' Loop through each line
For lc = 0 To UBound(sLines)
       ' calculate where to print based on justification
    Select Case pJustify
        ' right justify text
        Case Is = "R"
            TextLeftPos = (TabPos - PrOut.TextWidth(sLines(lc)))
        ' center justify text
        Case Is = "C"
            TextLeftPos = TabPos - PrOut.TextWidth(sLines(lc)) / 2
        Case Else ' L=left justify
            TextLeftPos = TabPos
    End Select
    PrOut.CurrentX = TextLeftPos ' set left position
    PrOut.Print sLines(lc);

' restore font
PrOut.Font.Size = SaveFontSize

End Sub

Public Sub SetScaleUnits()

' Setup scale units and margins

If ScaleUnits = "M" Then ' Metric (CM) or Inches
    TWIPS = 567 ' Metric
    TWIPS = 1440 ' Inches
End If

LineGapFactor = 1.08 ' Line height + Line Gap Factor of text height

' set margins at say 1.5 CM
LeftMargin = 1.5 * TWIPS
RightMargin = PrOut.Width - (1.5 * TWIPS)
CentreMargin = PrOut.Width / 2
TopMargin = 1.5 * TWIPS

End Sub

Public Sub SetZoomPercent(pZoom As Single)
ZoomPercent = pZoom
If ZoomPercent = 0 Then
    ZoomPercent = 100
End If

ZoomFactor = ZoomPercent / 100
End Sub

Private Sub Form_Load()

' first test the print to a picturebox
Dim PrintToPrinter As Boolean

PrintToPrinter = False ' set to screen first

If PrintToPrinter Then
    ' set the output for printer
    Set PrOut = Printer
    ' or for testing set to print to a picturebox to save paper
    Me.AutoRedraw = True
    Me.Move 0, 0, Screen.Width * 0.8
    Set PrOut = Me ' or a preview form for easy testing like frmPreview.picViewer
'    frmPreview.Show
'    frmPreview.Cls
End If

' do these first

SetZoomPercent 100 ' try setting zoom to 80 for 80% etc.
SetScaleUnits  ' set up the correct values for TWIPS for CM or Inches

PrOut.Font.Size = 14 ' set start/default font size

' Simple textbox example
'PrintLines LeftMargin, text1.Text

PrOut.CurrentY = TopMargin * ZoomFactor

' Print a simple textbox with lines of text
PrintLines LeftMargin, "Line 1" + vbCrLf + "Line 2" + vbCrLf + "Line 3" + vbCrLf

' More complex examples

Dim SaveCurrentBase As Single
Dim SaveCurrentTop As Single

' Now print Lines to the Left, Center and right
' first save the top of the print area

SaveCurrentTop = PrOut.CurrentY ' set the top of the print area CM down

' Print something on the Left margin

' create some multiline text

Dim lc As Long
Dim sText As String

For lc = 1 To 5
    sText = sText + "This is on the left line " + CStr(lc) + vbCrLf

PrintLines LeftMargin, sText
If PrOut.CurrentY > SaveCurrentBase Then
    SaveCurrentBase = PrOut.CurrentY
End If

' Print something on the rightmargin
PrOut.CurrentY = SaveCurrentTop ' restore top of print area
PrintLines RightMargin, "This is on the right", "R"
If PrOut.CurrentY > SaveCurrentBase Then
    SaveCurrentBase = PrOut.CurrentY
End If

' Print something in the middle
PrOut.CurrentY = SaveCurrentTop ' restore top of print area
PrintLines CentreMargin, "My Centred Text", "C"
If PrOut.CurrentY > SaveCurrentBase Then
    SaveCurrentBase = PrOut.CurrentY
End If

' Now Carry on printing from lowest point
PrOut.CurrentY = SaveCurrentBase

' print 3 cm from the left
PrintLines 3 * TWIPS, "Print Some Big Text", , 36 ' note the font setting

PrintLines LeftMargin, "Print Some Small Text", , 6

If PrintToPrinter Then
End If

End Sub

Open in new window

Martin LissOlder than dirtCommented:
Can you attach a zip file of your project or a demo project with the textbox and the code you are using to print it?
Hi pandgcs,  did you manage to solve your printing issue?

Do you need any explanation in the code example,  I can add more comments if required.
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.