?
Solved

Printing from a textbox

Posted on 2003-02-27
8
Medium Priority
?
209 Views
Last Modified: 2010-05-01
Hi Experts,

I have a form with a textbox on it. Its "multiline" property is set to true. Users can type text in it. At the end of the form, I have a button labeled "Print".

I cannot find a way for the printed text to fit the horizontal length of the paper. I would like that, each time a sentence cannot be fully printed on an horizontal line, instead of simply cutting it and not printing it, it would be continued on the next line of the page and so on with the complete content of the textbox. The best would be that a word would not be cut in the middle but entirely printed on the next line with the rest of the sentence.

If my question isn't clear enough, just tell me ! (english is not my mothertongue, sorry !).

Many thanx in advance.

Nico
0
Comment
Question by:Nicolas78
[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
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:webJose
ID: 8032500
0
 
LVL 5

Expert Comment

by:Julian_K
ID: 8032566
Hi, Nicolas.

Here is a procedure that does what you want.
Just send it a string, like "PrintText textbox1.text"


Private Sub PrintText(ByVal strText As String)
        Dim vntSplit As Variant
        Dim strAcc As String
        Dim sWidth As Single
    vntSplit = Split(strText, " ")
    sWidth = Printer.ScaleWidth
    For i = 0 To UBound(vntSplit)
        If Printer.TextWidth(strAcc & " " & vntSplit(i)) < sWidth Then
            strAcc = strAcc & " " & vntSplit(i)
            If i = UBound(vntSplit) Then Printer.Print strAcc
        Else
            Printer.Print strAcc
            strAcc = vntSplit(i)
        End If
    Next i
    If IsArray(vntSplit) Then Erase vntSplit
End Sub
0
 
LVL 5

Accepted Solution

by:
Julian_K earned 1600 total points
ID: 8032586
Oops, there is a bug. I fixed it here:

Private Sub PrintText(ByVal strText As String)
        Dim vntSplit As Variant
        Dim strAcc As String
        Dim sWidth As Single
    vntSplit = Split(strText, " ")
    sWidth = Printer.ScaleWidth
    For i = 0 To UBound(vntSplit)
        If Printer.TextWidth(strAcc & " " & vntSplit(i)) < sWidth Then
            strAcc = strAcc & " " & vntSplit(i)
'->>>CHANGED SECT BEGIN<<<-
            If i = UBound(vntSplit) Then
                Printer.Print strAcc
                strAcc = vntSplit(i)
            End If
'->>>CHANGED SECT END<<<-
        Else
            Printer.Print strAcc
            strAcc = vntSplit(i)
        End If
    Next i
    If IsArray(vntSplit) Then Erase vntSplit
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Nicolas78
ID: 8033821
Thank you webJose but I didn't really get your answer...

Many thanx, Julian. Most helpful !

Nico
0
 

Author Comment

by:Nicolas78
ID: 8033989
Excellent... Very useful. I just don't see how I can now adjust the left and right margin... But I'll dig around :-)
0
 
LVL 5

Expert Comment

by:Julian_K
ID: 8040322
Hi again....
If you need left and right margins, there are some small modifications...

'1.You have to declare 3 variables for margins:

Private sTopMargin As Single
Private sLeftMargin As Single
Private sRightMargin As Single

'2.There are 3 new and 1 modified lines in the code:

Private Sub PrintText(ByVal strText As String)
       Dim vntSplit As Variant
       Dim strAcc As String
       Dim sWidth As Single
   vntSplit = Split(strText, " ")
   
   'WAS:   sWidth = Printer.ScaleWidth
   sWidth = sRightMargin - sLeftMargin  'MODIFIED LINE

    'New LINE:
    Printer.CurrentY = sTopMargin
    For i = 0 To UBound(vntSplit)
       
        If Printer.TextWidth(strAcc & " " & vntSplit(i)) < sWidth Then
           
            strAcc = strAcc & " " & vntSplit(i)
       
            If i = UBound(vntSplit) Then
                'Position the text according the left margin:
                Printer.CurrentX = sLeftMargin  'NEW LINE
                Printer.Print strAcc
            End If
       
        Else
            'Position the text according the left margin:
            Printer.CurrentX = sLeftMargin  'NEW LINE
            Printer.Print strAcc
            strAcc = vntSplit(i)
        End If
       
    Next i
   
    If IsArray(vntSplit) Then Erase vntSplit
   
End Sub

Regards,
Julian.
0
 
LVL 5

Expert Comment

by:Julian_K
ID: 8040346
P.S. Of course, instead of this You could change the Printer's object PageWidth and ScaleLeft properties and so on, but I think it is not a good idea, becouse Then you'll have to store them before and restore them after printing, etc...
0
 

Author Comment

by:Nicolas78
ID: 8043793
Thanx. If you continue to do my job, I'll finally have to transfer you my salary :-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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