Visual Basic 6 printing

Hi,

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

Thanks

Andy
pandgcsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
0
inthedarkCommented:
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);
    
    PrintMoveDown
    
Next

' 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
Else
    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
Else
    ' or for testing set to print to a picturebox to save paper
    Me.AutoRedraw = True
    Me.Show
    Me.Move 0, 0, Screen.Width * 0.8
    
    
    DoEvents
    
    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
PrintMoveDown

' 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
Next

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
PrintMoveDown

' 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
    PrOut.EndDoc
End If



End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
inthedarkCommented:
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.
0
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.