Printer Problem

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.
PCavAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kesonlineConnect With a Mentor Commented:
Ok, I think I know what you can do. When you are passed the multiline input, there should be some vbCrLf's in text1.text, or whatever you're using. In the DLL, you take the string and make it into sub-strings, one for each vbCrLf. Then add the indentation as necessary to each line. That will work IF the user presses enter to get onto a line. If you're printing w/a fixed-width font (ie: Courier, Fixedsys, etc.), you can add a bit of code to the keypress handler of the textbox to add a vbcrlf after x characters on a line. This is not perfect, but its all I had time for (I'm currently working on a big DB app). Create a form with 1 textbox called text1.text, and give it a fixedwidth font:
Dim chOnLine As Integer
Dim maxChars As Integer
Dim wWrap As Boolean
Dim wWrapS As String
Sub reduceCO()
chOnLine = chOnLine - 1
If chOnLine < 0 Then chOnLine = 0
End Sub
Private Sub Form_Load()
maxChars = 30
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete Then chOnLine = chOnLine - 1
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbBack) Then
    reduceCO
ElseIf KeyAscii = Asc(vbNewLine) Then
    chOnLine = 0
ElseIf KeyAscii = Asc(" ") Then
   
    If chOnLine >= maxChars Then
        Text1.Text = Left$(Text1.Text, Len(Text1.Text) - Len(wWrapS) - 1) + vbNewLine + wWrapS
        Text1.SelStart = Len(Text1.Text)
        chOnLine = 0
        wWrap = False
        wWrapS = ""
    End If
    wWrapS = ""
Else
    chOnLine = chOnLine + 1
    wWrapS = wWrapS + Chr$(KeyAscii)
    If chOnLine >= maxChars Then
        wWrap = True
        'Text1.Text = Text1.Text + Chr$(KeyAscii) + vbNewLine
        'Text1.SelStart = Len(Text1.Text)
        'chOnLine = 0
        'KeyAscii = 0
    End If
End If
End Sub

I'll try to fix it up so it works better, but I think you get the idea.
0
 
vamsi_kCommented:
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.

Regards
0
 
PCavAuthor Commented:
I want to use the textbox control not the Richtextbox control
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zsiCommented:
Or, you could simply add a word-wrap feture to your printing routine.
0
 
mmahdiCommented:
Pcav,

OK, here waht you can try:

create a richtextbox control and make it invisible.
use:

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)

0
 
vamsi_kCommented:
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.  
0
 
PCavAuthor Commented:
The wordwrap thing is along the lines of what I'm looking for.
0
 
zsiCommented:
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)
         Wend
     
         sBuffer = sBuffer & Left(sTemp, pEnd - 1)
         Exit Do
      Else
         ' 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
         Next
     
         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)
            Wend
           
            ' 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
               Wend
            End If
         End If
         
         ' Copy the string section in to the buffer
         If bDone Then
            sBuffer = sBuffer & Trim(Mid(text, pStart, pEnd - pStart + 1))
         Else
            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

   Loop

   WordWrap = sBuffer
     
End Function




0
 
PCavAuthor Commented:
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)

0
 
zsiCommented:
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.
0
 
PCavAuthor Commented:
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.
0
 
PCavAuthor Commented:
Sorry the spaces doesn't work I realy do need to use a measurement.
0
 
raygibbinsCommented:
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

Private Type DOC_INFORMATION
    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


'*******************************************************************
0
 
zsiCommented:
ray,

Was that actually supposed to do something other than duplicate the functionality already present in VB's printer object?
0
 
JagarCommented:
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????
0
 
PCavAuthor Commented:
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.
0
 
kesonlineCommented:
What code are you using to indent the text?
0
 
JagarCommented:
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.
0
 
PCavAuthor Commented:
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.
0
 
kesonlineCommented:
Sure - send it over to me (ben@kescom.net) - thanks for the points.
0
All Courses

From novice to tech pro — start learning today.