Limit Number of Words Per Line By Number of Characters

Category:
String Manipulation
Type:
Snippets
Difficulty:
Beginning
Author:
Brad Gosdin

Version Compatibility: Visual Basic 6

More information:
This code will allow you to limit the number of words per line based on a length. For Example: If you have 72 characters per line it will figure out how many words will fit on that line until it wraps. I wrote this to format email messages, for the vbSendMail component I found on this site, because some email programs can only read 72 characters per line.

Example: After typing some text is a multi-line text box, you can add the following to a command button's click event to limit text to 72 characters per line:

Text1.Text = FormatByLength(Text1.Text, 72)

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: