We help IT Professionals succeed at work.

A very simple code that Prints from VB

nesifa
nesifa asked
on
Hi everyone,
I have a code as follows:

dim strPrint as string

strprint="this test has to be printed in printer"

How could I print the content of the string variable and send to printer.

thanks
- Nesifa
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014

Commented:
Printer.Print strprint
hes
CERTIFIED EXPERT

Commented:
Printer.Print strprint
Printer.EndDoc

Author

Commented:
how can you wrap the string if it is too long
hes
CERTIFIED EXPERT

Commented:
If the width of the text is greater than the maximum print width then you must separately process the text to either truncate it so that it fits the MaxWidth or split the lines suitably to simulate word-wrap.
CERTIFIED EXPERT
Top Expert 2014
Commented:
You can do something like the following:

Dim strPrint as String  'text to print
Dim strLine as String   'text that fits on a line
Dim intPosn As Integer
Dim intLoop As Integer
Dim intPrintWidth As Integer
With Printer
    .Font.Size = 10
    .ScaleMode = vbInches
    .CurrentY = 0.5
End With

intPrintWidth = Printer.ScaleWidth - 1  'avail print width
intPosn = 1
intLoop = 1
strPrint  = "Now is the time for all good men to come to the aid of their country.  The quick brown fox jumped over the lazy red dog on a hot Summer afternoon."

Do  'exhaust strPrint contents
    strLine = vbNullString
    Do  'fill line chars
        strLine = strLine & Mid$(strPrint, intPosn, 1)
        intPosn = intPosn + 1
    Loop Until (Printer.TextWidth(strLine) >= intPrintWidth) Or (intPosn = Len(strPrint))
    If intPosn = Len(strPrint) Then
    Else  'back off to word delimiter
        For intPosn = intPosn To intLoop Step -1
            Select Case Mid$(strLine, intPosn, 1)
                Case ",", ".", "-", " "
                    strLine = Left$(strLine, intPosn)
                    Exit For
            End Select
        Next
    End If
    Printer.CurrentX = 0.5  'indent
    Printer.Print strLine   'print line
    intPosn = intPosn + 1   'set up for next line
    intLoop = intPosn
   
Loop Until intLoop >= Len(strPrint)
Printer.EndDoc    'flush print buffer

===================================
Please note that this code doesn't accomodate page-crossing conditions.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.