Important alert: (current site time 6/20/2013 5:48:26 AM EDT)
 

VB icon

Printer Word Wrap

Email
Submitted on: 10/15/2012 5:22:33 PM
By: J.A. Coutts 
Level: Intermediate
User Rating: By 2 Users
Compatibility: VB 6.0
Views: 2701
 
     The Multiline Text Box or the Rich Text Box offers the ability to automatically word wrap long text at word boundaries, but no such feature exists when it is time to output that text to the printer. 3 possible solutions are presented here.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Printer Word Wrap
' Description:The Multiline Text Box or the Rich Text Box offers the ability to 
automatically word wrap long text at word boundaries, but no 
such feature exists when it is time to output that text to the printer. 
3 possible solutions are presented here.
' By: J.A. Coutts
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=74586&lngWId=1'for details.'**************************************

---------------------------------------------------------
Option 1 - Use the Microsoft recommended solution:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q146/0/22.asp&NoWebContent=1
This option is complex and lengthy, but does control the printer margins.
---------------------------------------------------------
Option 2 - Use something similar to what I developed:
'TextIn is the RichTextBox object, and iLen is the line length desired
Function WordWrap(TextIn As Object, iLen As Integer) As Variant
	Dim lStart As Long
	Dim lEnd As Long
	Dim N%
	Dim OutArray() As String
	lStart = 1
	'Make sure string is not zero length
	TextIn.Text = Trim(TextIn.Text)
	If Len(TextIn.Text) = 0 Then TextIn.Text = " "
	ReDim OutArray(Len(TextIn.Text) / iLen)
	With TextIn
	Do Until lStart >= Len(.Text)
		'Find actual end of line
		lEnd = InStr(lStart, .Text, vbCrLf)
		If lEnd - lStart < iLen Then
			'Line ends with CrLf within current line
			.SelStart = lEnd + iLen + 2
		Else
 			.SelStart = lStart + iLen
		End If
		If .SelStart - lStart >= iLen Then
			'line wraps, find preceding space
			.UpTo " ", False
		End If
		'If there are no spaces
		If .SelStart < lStart Then .SelStart = lStart + iLen
		OutArray(N%) = Mid$(.Text, lStart, .SelStart - lStart)
		'Debug.Print OutArray(N%)
		N% = N% + 1
		lStart = .SelStart + 1
 	Loop
	End With
	ReDim Preserve OutArray(N% - 1)
	WordWrap = OutArray
End Function
This option returns a variant array and only works with fixed width fonts 
such as Courier New or Arial. There is no guarantee that it is bug free, 
but it offers the ability to precede and follow with other printer instructions.
---------------------------------------------------------
Option 3 - Use the .SelPrint method in conjuction with a RichTextBox 
Control and a Common Dialog control:
	CMDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
	If RichTextBox1.SelLength = 0 Then
		CMDialog1.Flags = CMDialog.Flags + cdlPDAllPages
	Else
		CMDialog1.Flags = CMDialog.Flags + cdlPDSelection
	End If
	CMDialog1.ShowPrinter
	Printer.Print ""
	RichTextBox1.SelPrint CMDialog1.hDC
This option is by far the simplest and most elegant. The disadvantage is 
that youare stuck with only what is in the formatted RichTextBox, but even 
if you have to add text to the box before printing, and replace it with the 
original after, it is straight forward and simple.
---------------------------------------------------------


Other 13 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.