WordWrap




Option Explicit
Dim intLM As Integer
Dim intMaxLength As Integer
Private Sub Command1_Click()

Printer.ScaleMode = vbMillimeters
Printer.Scale (0, 0)-(210, 290)
Call PrintExample
Printer.EndDoc

End Sub

Private Sub Form_Load()

Dim strDummy As String

strDummy = "wie weet waar willem wouter woont?" & vbCrLf
strDummy = strDummy + "willem wouter woont weit weg."
Text1.Text = strDummy
Text2(0).Text = "15"
Text2(1).Text = "20"

End Sub

Private Sub PrintExample()

intLM = CInt(Text2(0).Text)
intMaxLength = CInt(Text2(1).Text)

Printer.CurrentX = intLM
Call CheckLine(Text1.Text)
End Sub

Public Sub Wrap(source As String)

Dim intCounter As Integer
Dim strPart As String
Dim intTeller As Integer

intCounter = 1
Do While intCounter < Len(source)
strPart = Mid$(source, intCounter, intMaxLength)
If intCounter + Len(strPart) < Len(source) Then
For intTeller = Len(strPart) To 1 Step -1
If Mid$(strPart, intTeller, 1) = Chr(32) Then
Printer.CurrentX = intLM
Printer.Print Mid$(source, intCounter, intTeller)
intCounter = intCounter + intTeller
intTeller = 1
End If
Next intTeller
Else
Printer.CurrentX = intLM
Printer.Print strPart
intCounter = Len(source)
End If
Printer.CurrentX = intLM
Loop
End Sub

Public Sub CheckLine(source As String)

Dim positie
Dim intLenght As Integer
Dim intCounter As Integer
Dim strDummy As String
intLenght = Len(source)
strDummy = source
'vanaf eerste teken tot aan het laatste teken

For intCounter = 1 To intLenght
'als teken is harde return

If Mid$(strDummy, intCounter, 1) = Chr(10) Then
If Len(Mid$(strDummy, 1, intCounter - 2)) > intMaxLength Then
Call Wrap(Mid$(strDummy, 1, intCounter - 2))
Else
Printer.CurrentX = intLM
Printer.Print Mid$(strDummy, 1, intCounter - 2)
End If
strDummy = Right$(strDummy, intLenght - intCounter)
intCounter = 1
intLenght = Len(strDummy)
End If
Next intCounter

If Len(strDummy) > intMaxLength Then
Call Wrap(strDummy)
Else
Printer.CurrentX = intLM
Printer.Print strDummy
End If
End Sub

make a new project with a form (name = frmWordWrap)
one textbox (name = text1)
two labels with an index (name = label1(0) & label1(1)
two textboxes with an index (name = text2(0) & text2(1)
a command button (name = command1)
insert the code
press F5










( wordwrap.html )- by Paolo Puglisi - Modifica del 17/12/2023