We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Printer Problem

PCav asked
Medium Priority
Last Modified: 2010-05-03
I have a text box that after the user types in the information I want to print it out.  I know how to use the printer object (at least the basics).  Here is the problem.

First if they type a lot of information and it goes on multiple lines, this is not represented in the print out.

Second if they hit enter the enter is represent, but it moves it back to the left margin.  I have this indented several inches on the paper.

Does anyone know how to go about printing out a textbox so that the print job looks like exactly what is in the textbox.
Watch Question

Get the text that was there in the textbox, and send it to printer using the Printer object.  Set the multiple line property of the TextBox to true.  Note that upto 256 characters can be there in the textbox, and the rest is ignored by the application if the text entered by the user exceeds this.  Another option is to use RichTextBox control, if you want the user to enter large content of data into the textbox, and you can print the contents of this using the below code

Printer.Print ""
RichTextBox1.SelPrint (Printer.hDC)

My advise is to use RichTextBox control, because it has more features as compared to the TextBox control.



I want to use the textbox control not the Richtextbox control

Or, you could simply add a word-wrap feture to your printing routine.


OK, here waht you can try:

create a richtextbox control and make it invisible.

RichTextBox1.Text = ""
Clipboard.SetText Text1.Text   'content of your textbox
RichText1.Text = Clipboard.GetText 'paste from textbox into Richtextbox
Printer.Print ""
RichText1.SelPrint (Printer.hDC)

Like I have said already, you could get the contents from the TextBox, and send it to the printer.  I think that textbox passes each character to the printer to print, and the printer in turn prints that character until it reaches the rightmost limit of the paper, and goes to next line to continue printing.  So, when the user presses "Enter" in the textbox, maybe the gap is filled with blank spaces and is being passed to the printer, and the printer is printing that, so alignment problem is coming.  Can u give me an example of what u want to get the user to type in the textbox.  


The wordwrap thing is along the lines of what I'm looking for.

Here ya go....


Function WordWrap(length As Integer, text As String) As String

   Dim sTemp As String
   Dim sBuffer As String
   Dim pStart As Integer
   Dim pEnd As Integer
   Dim l As Integer
   Dim c As String
   Dim x As Integer
   Dim bDone As Boolean

   l = Len(text)
   pStart = 1
   Do While pEnd < l
      pStart = pEnd + 1
      pEnd = pStart + length
      If l - pStart < length Then
         ' Trim any remaining Cr/Lf
         sTemp = Trim(Mid(text, pStart, l - pStart))
         pEnd = Len(sTemp)
         c = Mid(sTemp, pEnd, 1)
         While (c = Chr(10)) Or (c = Chr(13))
            pEnd = pEnd - 1
            c = Mid(sTemp, pEnd, 1)
         sBuffer = sBuffer & Left(sTemp, pEnd - 1)
         Exit Do
         ' If there are any hard breaks on this line, break on them
         ' Otherwise, wrap the line.
         bDone = False
         For x = pStart To pStart + length
            c = Mid(text, x, 1)
            If (c = Chr(10)) Or (c = Chr(13)) Then
               pEnd = x
               bDone = True
               Exit For
            End If
         If Not bDone Then
            ' Find the next previous space
            c = Mid(text, pEnd + 1, 1)
            While (c <> " ") And (pEnd > pStart)
               pEnd = pEnd - 1
               c = Mid(text, pEnd + 1, 1)
            ' If no previous space was found (pEnd = pStart), then find the next available space
            If (pEnd = pStart) And (pEnd < l) Then
               While (Mid(text, pEnd + 1, 1) <> " ") And (pEnd < l)
                  pEnd = pEnd + 1
            End If
         End If
         ' Copy the string section in to the buffer
         If bDone Then
            sBuffer = sBuffer & Trim(Mid(text, pStart, pEnd - pStart + 1))
            sBuffer = sBuffer & Trim(Mid(text, pStart, pEnd - pStart + 1)) & vbCrLf
         End If
      End If
      If pEnd = 0 Then
         MsgBox "pend = 0"
         Exit Function
      End If


   WordWrap = sBuffer
End Function


The problem with the above code is that when I have the text indented several inches.  When the wrap occurs it is moved over to the left hand margin.  I need to to stay at the indentation that I have it at.

Basically I need something like

sub PrintText(sub TextToPrint as string, XPosition as Long, YPosition as Long, Length as Long)


You can add a parameter to the funciton that stores the number of spaces to indent.  Subtract the number of spaces from the total line length, and add those spaces to each line before printing.  You can add them at the bottom of the routine.

Also, do know that this method will only work with fixed-pitch fonts (ie.. courier).  A word-wrap routine that works with any font, fixed or variable-pitch) is much more complex.


I know you don't happen to know of any book or something that would give me the code for that.  I wouldn't ask for it here unless I had a lot of points.  How many points do you think.  I'll probably be able to try this today and then I'll actually grade your answer.
Even though it technically only works for fixed length fonts it should be able to produce at least something accetable with other fonts.


Sorry the spaces doesn't work I realy do need to use a measurement.
Copy this code into a form containing a 2 text boxes, and a command button

txtName - to hold the printer name
txtText - to hold the Text to print (must be multiline)
cmdPrint - to print the Text

Option Explicit

    pDocName As String
    pOutputFile As String
    pDatatype As String
End Type

'**  Function Declarations:

Private Declare Function OpenPrinter& Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) ' Third param changed to long
Private Declare Function StartDocPrinter& Lib "winspool.drv" Alias "StartDocPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pDocInfo As DOC_INFO_1)
Private Declare Function StartPagePrinter& Lib "winspool.drv" (ByVal hPrinter As Long)
Private Declare Function WritePrinter& Lib "winspool.drv" (ByVal hPrinter As Long, pBuf As Any, ByVal cdBuf As Long, pcWritten As Long)
Private Declare Function EndDocPrinter& Lib "winspool.drv" (ByVal hPrinter As Long)
Private Declare Function EndPagePrinter& Lib "winspool.drv" (ByVal hPrinter As Long)
Private Declare Function ClosePrinter& Lib "winspool.drv" (ByVal hPrinter As Long)

Private Sub cmdPrint_Click()
    Dim hPrinter&
    Dim jobid&
    Dim res&
    Dim written&
    Dim printdata$
    Dim docinfo As DOC_INFORMATION
    res& = OpenPrinter(txtName.Text, hPrinter, 0)
    If res = 0 Then
        MsgBox "Could not to open the printer"
        Exit Sub
    End If
    docinfo.pDocName = "MyDoc"
    docinfo.pOutputFile = vbNullString
    docinfo.pDatatype = vbNullString
    jobid = StartDocPrinter(hPrinter, 1, docinfo)
    Call StartPagePrinter(hPrinter)
    printdata$ = txtText.Text & vbFormFeed
    Call WritePrinter(hPrinter, ByVal printdata$, Len(printdata$), written)
    Call EndPagePrinter(hPrinter)
    Call EndDocPrinter(hPrinter)
    Call ClosePrinter(hPrinter)

End Sub

Private Sub Form_Load()
    txtName.Text = "HP LaserJet 4"
End Sub



Was that actually supposed to do something other than duplicate the functionality already present in VB's printer object?

This will write to the printer exactly the way it is in the textbox, even a multi-line text, including if the print is to be indented several inches????


Since you won't answer the question above and others say this only mimicks the functionality of the VB Printer Object I have to re-open this.
What code are you using to indent the text?

It's part of the DLL that I wrote to simplify the Printer Object.  So Before I print out the line I know how far I want to indent it.  So I have the indent in inches passed to it as a variable.
Unlock this solution and get a sample of our free trial.
(No credit card required)


I actually wrote the function that I was looking for, but I'll give you the points anyways.  Let me know if you are interested in the code to do what I want.  This handles Enters and the specified width.  It will automatically move the text down to the next line, handle enters and everything, it's really pretty cool.
Sure - send it over to me (ben@kescom.net) - thanks for the points.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.