Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Printing from a textbox

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
Nicolas78
Asked:
Nicolas78
  • 4
  • 3
1 Solution
 
Julian_KCommented:
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
 
Julian_KCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Nicolas78Author Commented:
Thank you webJose but I didn't really get your answer...

Many thanx, Julian. Most helpful !

Nico
0
 
Nicolas78Author Commented:
Excellent... Very useful. I just don't see how I can now adjust the left and right margin... But I'll dig around :-)
0
 
Julian_KCommented:
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
 
Julian_KCommented:
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
 
Nicolas78Author Commented:
Thanx. If you continue to do my job, I'll finally have to transfer you my salary :-)
0

Featured Post

Technology Partners: 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!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now