VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Word Wrap Printing

by Andrew Mallner (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

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

Rate Word Wrap Printing

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

Download this snippet    Add to My Saved Code

Word Wrap Printing Comments

No comments have been posted about Word Wrap Printing. Why not be the first to post a comment about Word Wrap Printing.

Post your comment

Subject:
Message:
0/1000 characters