Results 1 to 14 of 14

Thread: Problem Printing from a Text Box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Question Problem Printing from a Text Box

    VB Code:
    1. Private Sub cmdPrint_Click()
    2.     'Print the Description
    3.     Printer.Font.Name = "Times New Roman"
    4.     Printer.Font.Size = 11
    5.     Printer.Print  Text1.Text
    6.     Printer.EndDoc

    I wrote these code to print the description in the text box and the output is like it can't print the text as what it shown in the text box or text file (The descriptions were stored in a text file). Certain lines in a paragraph were skipped until a suppose to be long document (description) printed as short/uncompleted. Seems like the printer can't "wrap" the paragraph to make the text fit to the page.

    Thanks in advance!
    Last edited by hbin; Sep 25th, 2004 at 04:47 AM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    My initial reaction would be that you would want to SPLIT the string in the text box using vbCr or VbCrLf - not sure what you have for a line-break in the text...

    Once you SPLIT the string into the ARRAY, then print one line at a time.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Hi szlamany,
    I have a list box where the user select an item from the list box, and the description of the item will be display in a text box. Different item will display different description in the same text box. Refer http://www.vbforums.com/showthread.p...xtbox+Resolved

    These are the code I use to separate the description:
    VB Code:
    1. Dim desc() As String
    2.  
    3. Private Sub Form_Load()
    4. Dim FF As Long
    5. Dim myStr As String
    6. FF = FreeFile()
    7. Open "C:\test.txt" For Input As #FF
    8. myStr = Input(LOF(FF), FF)
    9. desc = Split(myStr, "{")
    10. Close #FF
    11. End Sub
    12.  
    13.  
    14. Private Sub List1_Click()
    15. Text1.Text = desc(List1.ListIndex)
    16. End Sub

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Ok - I think I understand your question.

    You have a string that is too big for the printer to print on one line.

    So you need to cut it up.

    I ripped this from a program we have here - I had to make edits in NOTEPAD to remove a whole lot of other stuff it was doing - hopefully it will work for you as is. x, y and z are dim'd long and strPara is the string to print.

    VB Code:
    1. Do While Len(strPara)
    2.  
    3.     y = Printer.Width   ' Width in twips of printer
    4.    
    5.     If Printer.TextWidth(strPara) > y Then  ' Size of what we want to print is too big
    6.        
    7.         z = Len(strPara)    ' Length of it in characters
    8.        
    9.         Do While Printer.TextWidth(Left(strPara, z)) > y
    10.             z = z - 1       ' Backup until it will fit
    11.         Loop
    12.        
    13.         y = z           ' Position that fits
    14.        
    15.         z = Asc(Mid(strPara, y + 1, 1)) ' ASC value of next character
    16.    
    17.         If z <> 32 Then     ' That next character is not a space
    18.             z = Asc(Mid(strPara, y, 1))
    19.             Do While z <> 32    ' Start backing up till we find the space
    20.                 y = y - 1
    21.                 z = Asc(Mid(strPara, y, 1))
    22.             Loop
    23.         End If
    24.         Printer.Print Trim(Left(strPara, y))
    25.         strPara = Mid(strPara, y + 1)
    26.     Else
    27.         Printer.Print Trim(strPara)
    28.         Exit Do
    29.     End If
    30. Loop

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Hi szlamany,

    Thanks for taking your time to modified those code for me, I tried them but it seems like can't work.

    I paste the code into the "Print" button and it has no reaction when I clicked "Print", so I add
    VB Code:
    1. Printer.Print  Text1.Text
    The printer takes a long time before it prints, but it still can't print the text correctly.

    I tried to replace strPara with Text1.Text but it still can't works.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    strPara is the string that you want to print.

    "str" is a standard prefix for a string variable - lots of people use it.

    Here's more clear code for you:

    VB Code:
    1. Dim x as Long, y as Long, z as Long
    2. Dim strPara as String
    3.  
    4. strPara = Text1.Text   '  <<<< Add this line of code - move the Text1.Text box data into the strPara string variable
    5.  
    6. Do While Len(strPara)
    7.  
    8.     y = Printer.Width   ' Width in twips of printer
    9.    
    10.     If Printer.TextWidth(strPara) > y Then  ' Size of what we want to print is too big
    11.        
    12.         z = Len(strPara)    ' Length of it in characters
    13.        
    14.         Do While Printer.TextWidth(Left(strPara, z)) > y
    15.             z = z - 1       ' Backup until it will fit
    16.         Loop
    17.        
    18.         y = z           ' Position that fits
    19.        
    20.         z = Asc(Mid(strPara, y + 1, 1)) ' ASC value of next character
    21.    
    22.         If z <> 32 Then     ' That next character is not a space
    23.             z = Asc(Mid(strPara, y, 1))
    24.             Do While z <> 32    ' Start backing up till we find the space
    25.                 y = y - 1
    26.                 z = Asc(Mid(strPara, y, 1))
    27.             Loop
    28.         End If
    29.         Printer.Print Trim(Left(strPara, y))
    30.         strPara = Mid(strPara, y + 1)
    31.     Else
    32.         Printer.Print Trim(strPara)
    33.         Exit Do
    34.     End If
    35. Loop

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Yup, the printer now can wrap the text to next line for printing, but the printing is too near to the edge of paper and there's some words that near to the edge missing. I used an A4 paper for printing.

    BTW, after I clicked “Print”, the printer takes about 1 minute to process before it started to print, is this normal?

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Printer.EndDoc will close the printer and print immediately.

    Add it just under the bottom of the loop.

    If things print too close, then change this line:

    VB Code:
    1. y = Printer.Width   ' Width in twips of printer

    to something like:
    VB Code:
    1. y = Printer.Width - 1440 ' Width in twips of printer - minus one inch

    1440 twips represents an inch. Use half that value for 1/2 inch.

    Or post your code so I can look at it...

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    VB Code:
    1. Private Sub cmdPrint_Click()
    2.     'Print the Description
    3.  
    4.     Printer.Print Tab(10)    
    5.     Printer.Font.Size = 10
    6.    
    7.     Dim x As Long, y As Long, z As Long
    8.     Dim strPara As String
    9.  
    10. strPara = Text1.Text   '  <<<< Add this line of code - move the Text1.Text box data into the strPara string variable
    11.  
    12. Do While Len(strPara)
    13.  
    14.     y = Printer.Width - 720   ' Width in twips of printer
    15.    
    16.     If Printer.TextWidth(strPara) > y Then  ' Size of what we want to print is too big
    17.        
    18.         z = Len(strPara)    ' Length of it in characters
    19.        
    20.         Do While Printer.TextWidth(Left(strPara, z)) > y
    21.             z = z - 1       ' Backup until it will fit
    22.         Loop
    23.        
    24.         y = z           ' Position that fits
    25.        
    26.         z = Asc(Mid(strPara, y + 1, 1)) ' ASC value of next character
    27.    
    28.         If z <> 32 Then     ' That next character is not a space
    29.             z = Asc(Mid(strPara, y, 1))
    30.             Do While z <> 32    ' Start backing up till we find the space
    31.                 y = y - 1
    32.                 z = Asc(Mid(strPara, y, 1))
    33.             Loop
    34.         End If
    35.         Printer.Print Trim(Left(strPara, y))
    36.         strPara = Mid(strPara, y + 1)
    37.     Else
    38.  
    39.         Printer.Print Trim(strPara)
    40.         Exit Do
    41.     End If
    42. Loop
    43.  
    44. Printer.EndDoc
    45.  
    46. End Sub

    After adding the Printer.EndDoc it still takes about 1 minute to process. Yup, this code works
    VB Code:
    1. y = Printer.Width - 720
    but how can I indent/tab the line to make it not too close to the edge of left?
    VB Code:
    1. Printer.Print Tab (10)
    seems like can't work.

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    If you are going to use TEXTWIDTH - which is a twip-level length function, then you should not use TAB().

    Instead set .CURRENTX to a twip value that represents the position form the left side that you want to indent.

    This position that you use - this value - would most likely also be the number to subtract from the PRINTER.WIDTH - since that is the "offset" from the left-side that you are using.

    The one-minute thing makes no sense - try adding a PRINTER.NEWPAGE before the .ENDDOC to see if it prints/ejects the page immediately - just for a test. Don't leave the .NEWPAGE in there, unless you really want to print a second page.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    I added Printer.CurrentX = 720 before Printer.Font.Size = 10 and it only indent the first line of the description. If I put Printer.CurrentX = 720 before y = Printer.Width - 720, then the left indention goes some kind like randomly. Since what I use in the left-side will be "offset" from the PRINTER.WIDTH, it's pretty hard to balance both side's indention.

    I added .NEWPAGE before the .ENDDOC, but it still takes about 1 minute to process, this is weird.

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    CurrentX must be prior to printing each line. The PRINTER.PRINT method, without a ";" trailing the string, will increment the CURRENTY to the next print line and set CURRENTX to zero.

    Since you are setting CURRENTX before printing EACH line, do not use TAB() - do a direct PRINTER.PRINT xxx where xxx represents the line (string) you want to print.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    So where should I put the PRINTER.CURRENTX = 720 ?
    do a direct PRINTER.PRINT xxx where xxx represents the line (string) you want to print.
    I can't do a direct printing because the String is too big. BTW, I didn't use any TAB()

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Question

    Hi there,

    I'm still having problem with the left indention and it takes a long time for the printer to process before it print. I guess maybe the String is too big, because I had tried to print a description with less text and the printer print immediately; but when I tried to print a description with lots of text, it takes about 1~2 minutes to process before the printer start to print.

    Thanks in advance!
    Attached Files Attached Files
    Last edited by hbin; Sep 23rd, 2004 at 10:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width