Print Preview ActiveX Active Users: 70 / Visits Today: 75
Highest Active Users: 70
Print Preview ActiveX
Home | Profile | Register | Active Topics | Members | Search | FAQ | RSS
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Print Preview
 Tips & Tricks
 VB Routine: getNumLines
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic: VB Code Snippet: fit plain text in area Topic Next Topic: VB Routine: paraTextFit  

wbgookin
Junior Member

USA
113 Posts

Posted - 11/20/2004 :  02:43:42  Show Profile  Visit wbgookin's Homepage  Reply with Quote
Here's a routine I use to figure the number of lines it will take to print plain text, given a width. It works for me, but your mileage may vary.


Function getNumLines(prn As PrintPreview5.Preview, ByVal printText As String, ByVal fitWidth As Variant) As Integer
    'This routine will determine how many lines a plain text string will take to print given a width
    'Variables:
    'prn is the previously defined preview control
    'printText is the text string to be printed
    'fitWidth is the width that you want to print the text in
    'Returns:
    'getNumLines returns the number of lines (integer)
    
    'Dimension a few variables.
    'i is a counter
    'splitText() is an array that will be filled with each word (space delimited)
    'tmpText is a junk variable
    Dim i As Integer
    Dim splitText() As String
    Dim tmpText As String

    'See how many lines it is, given the width
    'Begin with one line
    getNumLines = 1
    
    'Split the text into the splitText array (space delimited)
    splitText = Split(printText, " ")
    
    'Initialize our temp string
    tmpText = ""
    
    'Loop through the array of words, adding one at a time to the tmpText variable
    'Each loop, check the text width.  If it is still less than the fitWidth, then keep adding words.
    'If the text width is greater than the fit width, remove the last word added, add a line to getNumLines,
    'and clear the tmpText variable.  Keep going until all the words have been added.
    For i = LBound(splitText) To UBound(splitText)
        tmpText = Trim(tmpText & " " & splitText(i))
        If prn.TextWidth(tmpText) > prn.C_Twips(fitWidth) Then
            getNumLines = getNumLines + 1
            tmpText = splitText(i)
        End If
    Next i

    'Now, in case the string is only one word, make sure it only returns one line
    If Trim(splitText(0)) = Trim(printText) Then
        getNumLines = 1
    End If

End Function
Go to Bottom of Page
  Previous Topic: VB Code Snippet: fit plain text in area Topic Next Topic: VB Routine: paraTextFit  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Print Preview ActiveX © Mandix Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000 Version 3.4.02