Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi_Line Textbox Printing

Status
Not open for further replies.

Guygateway

Programmer
Mar 8, 2001
7
US
Morning VBers.

My latest problem, is that I have a textbox, with the Multi_Line property set to true, which lets me enter, save, and retrieve the Multi_lines all right(3 lines max.), but when I go to print them out (via Printer.Print), I get one long line strung out to the edge of the page.

I entered 3 lines of "a" 's, to test this thing out (about 3 lines of 70 characters each), and what I get is one long line of "a" 's, that falls way short of the 210 entered.

So, I don't get Multi_Lines printed, nor do I even get all of the data entered, printed.

Can anyoe help me fix this? Thanks in advance.

Guy Hurt
Guygateway@Yahoo.COM
 
Afternoon!

I have recently had the exact same problem. It is not an easy thing to word wrap the text, put the code at the bottom of this post into a class. I only put it together quickly so it might not be very pretty code!

You will notice an escape sequence that is the start of each line (the text box I have used is not visible). This escape sequence contains details to make the print out look nice (ie Bold, Underlined, Fontsize) you can get rid of this bit if it's not required.

Let me know if this is useful.

Cheers,

Mark

###########


Option Explicit

'Default margin values
Private Const MARGIN_LEFT = 720
Private Const MARGIN_RIGHT = 720
Private Const MARGIN_TOP = 1440
Private Const MARGIN_BOTTOM = 1440

'Modifiable parameters
Private m_HeaderText As String
Private m_MarginLeft As Single
Private m_MarginTop As Single
Private m_MarginRight As Single
Private m_MarginBottom As Single

Public Property Let HeaderText(sText As String)
m_HeaderText = sText
End Property

Public Property Get HeaderText() As String
HeaderText = m_HeaderText
End Property

Public Property Let MarginLeft(nMargin As Single)
m_MarginLeft = nMargin
End Property

Public Property Get MarginLeft() As Single
MarginLeft = m_MarginLeft
End Property

Public Property Let MarginTop(nMargin As Single)
m_MarginTop = nMargin
End Property

Public Property Get MarginTop() As Single
MarginTop = m_MarginTop
End Property

Public Property Let MarginRight(nMargin As Single)
m_MarginRight = nMargin
End Property

Public Property Get MarginRight() As Single
MarginRight = m_MarginRight
End Property

Public Property Let MarginBottom(nMargin As Single)
m_MarginBottom = nMargin
End Property

Public Property Get MarginBottom() As Single
MarginBottom = m_MarginBottom
End Property

'Public method to print text
Public Sub PrintText(sText As String)
Dim i As Integer, j As Integer, sCurrWord As String

Screen.MousePointer = vbHourglass
'Initialize first page
DoNewPage False
'Print text, word-wrapping as we go
i = 1
Do Until i > Len(sText)
'Get next word
sCurrWord = ""
Do Until i > Len(sText) Or Mid$(sText, i, 1) <= &quot; &quot;
sCurrWord = sCurrWord & Mid$(sText, i, 1)
i = i + 1
Loop

' If it is our escape sequence then look at the settings
If Left(sCurrWord, 6) = &quot;&~~**#&quot; Then

' Bold?
Select Case Mid(sCurrWord, 7, 1)
Case &quot;Y&quot;
Printer.FontBold = True
Case Else
Printer.FontBold = False
End Select

' Underlined?
Select Case Mid(sCurrWord, 8, 1)
Case &quot;Y&quot;
Printer.FontUnderline = True
Case Else
Printer.FontUnderline = False
End Select

' Font size
Printer.FontSize = Mid(sCurrWord, 9, 2)

' Left margin
Select Case Mid(sCurrWord, 11, 1)
Case &quot;M&quot;
'Printer.CurrentX = Mid(sCurrWord, 12, 4)
m_MarginLeft = Mid(sCurrWord, 12, 4)
Printer.CurrentX = m_MarginLeft
End Select

i = i + 1

Else


'Check if word will fit on this line
If (Printer.CurrentX + Printer.TextWidth(sCurrWord)) > (Printer.ScaleWidth - m_MarginRight) Then
'Send carriage-return line-feed to printer
Printer.Print
'Check if we need to start a new page
If Printer.CurrentY > (Printer.ScaleHeight - m_MarginBottom) Then
DoNewPage
Else
Printer.CurrentX = m_MarginLeft
End If
End If
'Print this word

Printer.Print sCurrWord;
'Process whitespace and any control characters
Do Until i > Len(sText) Or Mid$(sText, i, 1) > &quot; &quot;
Select Case Mid$(sText, i, 1)
Case &quot; &quot; 'Space
Printer.Print &quot; &quot;;
Case Chr$(10) 'Line-feed
'Send carriage-return line-feed to printer
Printer.Print
'Check if we need to start a new page
If Printer.CurrentY > (Printer.ScaleHeight - m_MarginBottom) Then
DoNewPage
Else
Printer.CurrentX = m_MarginLeft
End If
Case Chr$(9) 'Tab
j = (Printer.CurrentX - MARGIN_LEFT) / Printer.TextWidth(&quot;0&quot;)
j = j + (10 - (j Mod 10))
Printer.CurrentX = MARGIN_LEFT + (j * Printer.TextWidth(&quot;0&quot;))
Case Else 'Ignore other characters
End Select
i = i + 1
Loop

End If
Loop
Printer.EndDoc
Screen.MousePointer = vbDefault
End Sub

'Prints page header and footer
Private Sub DoNewPage(Optional bEjectPage As Boolean = True)
Dim Buff As String

'Start new page if requested
If bEjectPage Then
Printer.NewPage
End If
'Print page header

Printer.FontSize = 14
Printer.FontBold = True
Printer.FontUnderline = True

Printer.CurrentY = (m_MarginTop - Printer.TextHeight(m_HeaderText)) / 2
Printer.CurrentX = ((Printer.ScaleWidth - Printer.TextWidth(m_HeaderText)) / 2) '- 1500

Printer.Print m_HeaderText;

Printer.FontSize = 10
Printer.FontBold = False
Printer.FontUnderline = False

'Print page footer
Buff = Format$(Now, &quot;DD/MM/YYYY HH:MM:SS&quot;)
Printer.CurrentX = m_MarginLeft
Printer.CurrentY = Printer.ScaleHeight - (m_MarginBottom / 2)
Printer.Print Buff;
Buff = &quot;Page &quot; & CStr(Printer.Page)
Printer.CurrentX = Printer.ScaleWidth - (Printer.TextWidth(Buff) + m_MarginRight)
Printer.CurrentY = Printer.ScaleHeight - (m_MarginBottom / 2)
Printer.Print Buff;
'Reset position to top of page
Printer.CurrentX = m_MarginLeft
Printer.CurrentY = m_MarginTop
End Sub

Private Sub Class_Initialize()
'Set default properties
m_MarginLeft = MARGIN_LEFT
m_MarginRight = MARGIN_RIGHT
m_MarginTop = MARGIN_TOP
m_MarginBottom = MARGIN_BOTTOM
m_HeaderText = &quot;Title of page&quot;
End Sub
 
Dear MadLarry,

Wow, thanks a Lot. I wasn't expecting anything like the code you posted in your response.

I'll have to try to carefully intergrate your code into mine. Again, thanks.

Guy Hurt
Guygateway@YAHOO.COM
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top