Solved

Printer Problem

Posted on 1998-06-08
20
247 Views
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.
0
Comment
Question by:PCav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
  • +4
20 Comments
 

Expert Comment

by:vamsi_k
ID: 1462816
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
 

Author Comment

by:PCav
ID: 1462817
I want to use the textbox control not the Richtextbox control
0
 
LVL 4

Expert Comment

by:zsi
ID: 1462818
Or, you could simply add a word-wrap feture to your printing routine.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Expert Comment

by:mmahdi
ID: 1462819
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
 

Expert Comment

by:vamsi_k
ID: 1462820
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
 

Author Comment

by:PCav
ID: 1462821
The wordwrap thing is along the lines of what I'm looking for.
0
 
LVL 4

Expert Comment

by:zsi
ID: 1462822
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
 

Author Comment

by:PCav
ID: 1462823
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
 
LVL 4

Expert Comment

by:zsi
ID: 1462824
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
 

Author Comment

by:PCav
ID: 1462825
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
 

Author Comment

by:PCav
ID: 1462826
Sorry the spaces doesn't work I realy do need to use a measurement.
0
 
LVL 1

Expert Comment

by:raygibbins
ID: 1462827
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
 
LVL 4

Expert Comment

by:zsi
ID: 1462828
ray,

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

Expert Comment

by:Jagar
ID: 1462829
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
 

Author Comment

by:PCav
ID: 1462830
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
 
LVL 1

Expert Comment

by:kesonline
ID: 1462831
What code are you using to indent the text?
0
 
LVL 2

Expert Comment

by:Jagar
ID: 1462832
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
 
LVL 1

Accepted Solution

by:
kesonline earned 70 total points
ID: 1462833
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
 

Author Comment

by:PCav
ID: 1462834
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
 
LVL 1

Expert Comment

by:kesonline
ID: 1462835
Sure - send it over to me (ben@kescom.net) - thanks for the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question