Search Tools Links Login

Word Wrap Printing

Posted: 2002-06-01
By: ArchiveBot
Viewed: 86

Filed Under:

VB6 Code Cache

No attachments for this post


This is a subroutine to automatically wrap a text string in whole words to a printer.

Original Author: Andrew Mallner

Code

Private Sub PrintWordWrap(YourText As String, LeftMargin_InTwips As Long, RightMargin_InTwips As Long)
On Error GoTo Errors
Start = 1
Char = ""
TempText = ""
Dim boolSpace As Boolean
For Location = 1 To Len(YourText)
Char = Mid(YourText, Location, 1)
If Char = " " Then
If Printer.TextWidth(TempText2 & Mid(YourText, Start, Location - Start)) <= Printer.Width - RightMargin_InTwips - LeftMargin_InTwips - 700 Then
  TempText = Mid(YourText, Start, Location - Start)
  Pos = Location
  boolSpace = True
Else
  Start = Location
  Pos2 = Location
  Printer.CurrentX = LeftMargin_InTwips
  Printer.Print TempText2 & TempText
  TempText2 = Mid(YourText, Pos + 1, Location - Pos - 1)
  TempText = ""
  boolSpace = False
End If
ElseIf Char = vbCr And Mid(YourText, Location + 1, 1) = vbLf And Printer.TextWidth(TempText2 & Mid(YourText, Start, Location - Start)) <= Printer.Width - RightMargin_InTwips - LeftMargin_InTwips - 700 Then

If Not InStr(Mid(YourText, Start, Location - Start), vbCr) <> 0 Then
Printer.CurrentX = LeftMargin_InTwips
End If
Printer.Print TempText2 & Mid(YourText, Start, Location - Start);
Start = Location + 1
Pos2 = Location
TempText = ""
TempText2 = ""
boolSpace = False
ElseIf boolSpace = False And _
  Printer.TextWidth(Mid(YourText, Start, Location - Start)) >= Printer.Width - Printer.TextWidth("W") - RightMargin_InTwips - LeftMargin_InTwips - 700 And _
  Printer.TextWidth(Mid(YourText, Start, Location - Start)) < Printer.Width - RightMargin_InTwips - LeftMargin_InTwips - 700 Then

Printer.CurrentX = LeftMargin_InTwips
Printer.Print Mid(YourText, Start, Location - Start)
Start = Location
Pos = Location
TempText = ""
TempText2 = ""

End If
If Printer.CurrentY > Printer.Height Then Printer.NewPage
Next
If Printer.TextWidth(TempText2 & TempText) <= Printer.Width - RightMargin_InTwips - LeftMargin_InTwips - 700 Then
Printer.CurrentX = LeftMargin_InTwips
Printer.Print TempText2 & Mid(YourText, Pos2, Location - Pos2);
End If
Printer.EndDoc
Exit Sub
Errors:
boxit = MsgBox(Err.Description, vbOKOnly + vbApplicationModal + vbInformation, Err.Source & " Error #" & Err.Number)
' 700 twips are subtracted from the width of the
' page to account for the non-printable area for
' MY printer. I don't know for sure, but this may
' vary depending on your printer.
End Sub


Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.