preload

How to wrap text line (QTP, VBScript)

Posted by Albert Gareev on Feb 25, 2010 | Categories: How toText Data

Parent page: Service Functions – String (QTP, VBScript)

Question

The question was asked here.

Hi Albert!

Can you email me a QTP function that wraps line of text without breaking words?

Thank you! 

Answer

Proper text alignment is important. The whole layout of CSS block or HTML table could be broken if a text line won’t fit.

The function below performs wrapping of a text line without breaking words, at the nearest space character. However, if required length exceeds length of a single word then wrapping will be forced at max allowed length. Line length shorter than 3 chars is considered an invalid input.

Implementation

Description

Validate input – allocate memory.

Cut text into pieces of required length – adjust line length towards nearest space character.

WrapTextLine Function

Public  Function WrapTextLine(ByVal sTexLine, ByVal intMaxLen, ByRef dvLines)

 Dim intLoc, intCount, Iter
 Dim sLine, sResult

 If intMaxLen < 3 Then
  WrapTextLine = -1
  Exit Function
 End If

 If Len(sTexLine) < intMaxLen Then
  WrapTextLine = 1
  dvLines = Array(sTexLine)
  Exit Function
 End If
  
 intCount = Int(Len(sTexLine) / intMaxLen)+1
 dvLines = Array("")
 RedIm dvLines(intCount)
 Iter = 0

 Do While Len(sTexLine) > 0
  
  If Iter > intCount Then
   RedIm Preserve dvLines(Iter)
  End If

  sLine = Left(sTexLine, intMaxLen)
  intLoc = InStrRev(sLine," ", Len(sLine))

  If Len(sLine) < intMaxLen Then
   sTexLine = ""
   dvLines(Iter) = sLine
   Iter = Iter + 1
   Exit Do
  End If

  If intLoc = 0 Then
   sTexLine = Mid(sTexLine, intMaxLen+1)
   dvLines(Iter) = sLine
   Iter = Iter + 1
  Else
   sResult = Left(sLine, intLoc)
   sTexLine = Mid(sLine, intLoc+1) & Mid(sTexLine, intMaxLen+1)
   dvLines(Iter) = sResult
   Iter = Iter + 1
  End if
 Loop
WrapTextLine = Iter
End Function
'
Dim sLine, dvLine, intLineCount

'sLine = "Lorem ipsum dolor sit amet, adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. Lorem ipsum dolor sit amet, emphasis consectetuer adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus."

sLine = "Loremipsumdolorsitamet,adipiscingelit.Nullamdignissimconvallisest. Quisquealiquam.Donecfaucibus.Nunciaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus. Lorem ipsum dolor sit amet, emphasis consectetuer adipiscing elit. Nullam dignissim convallis est. Quisque aliquam. Donec faucibus. Nunc iaculis suscipit dui. Nam sit amet sem. Aliquam libero nisi, imperdiet at, tincidunt nec, gravida vehicula, nisl. Praesent mattis, massa quis luctus fermentum, turpis mi volutpat justo, eu volutpat enim diam eget metus. Maecenas ornare tortor. Donec sed tellus eget sapien fringilla nonummy. Mauris a ante. Suspendisse quam sem, consequat at, commodo vitae, feugiat in, nunc. Morbi imperdiet augue quis tellus."

intLineCount = WrapTextLine(sLine, 50, dvLine)

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.