Using the dialog, users can set the page orientation, which also sets the Printer object's orientation. Changing the page orientation swaps the page's width and height automatically, so you don't need to add any other code to handle the printout's orientation.
To print the text entered into the TextBox control, start by breaking the control's contents into an array of strings. Each element of the array contains a single paragraph of text. If the text consisted of very short paragraphs, you could print each paragraph on a single line by passing an element of this array to the Print method as an argument. Usually, however, you have to manually break text into shorter strings, each of which must fit in a single line on the page.
To create readable text, each line must end on a word boundary, so that each string fits in the width between the left and right margins on the page. Overall, the code extracts each word to be printed from the complete text, calculates its width and then—if that word fits on the current line—adds it to the current line to be printed. Otherwise, it prints the current line contents and then starts a new line by setting the CurrentX and CurrentY properties of the output device. The GetNextWord() function controls how the code determines which portion of the remaining text constitutes the "next word."
While pos <= Len(str) And Mid(str, pos, 1) <> " "
While pos <= Len(str) And Mid(str, pos, 1) = " "
The GetNextWord() function extracts the next word from the string str, starting at location pos. Note that trailing punctuation and trailing spaces are considered part of the current word, and not as leading spaces for the following word. Tabs, carriage returns and other white space characters are not treated as spaces, and thus are not part of the current word.
The core of the text printing code is quite straightforward. The While loop retrieves the next word from the text and appends it to the newitem variable. The process continues until the code reaches the end of the text. If the combined length of the newitem string and the new word doesn't fit on the current line, the code prints the current value of newitem and then starts a new line, by setting the CurrentX property to the left margin. The next word, which hasn't been printed yet, is assigned to the variable newitem. Each line will contain at least one word. This simple example code assumes that the longest word in the document fits in the width of the page. If the code runs into a very long word, it will not break it into smaller segments. You should address that condition if you plan to use the code to print long words within tight margins, perhaps by adding a hyphenation routine.
The Print method sets the CurrentY property automatically, increasing it by one line (the height of the current font) each time you print something. The constant LineContSymbol is the character you may wish to print at the end of a line to indicate that it's been broken. It's a blank string for regular text, but you can set it to any symbol for program listings and other special types of printouts.
One of the parameters affecting the appearance of the text on the TextBox is the control's Alignment property. The PrintAlignedText() subroutine, shown below, accepts two arguments: the string to be printed and a constant that indicates the alignment of the string on the line (this constant is the value of the txtMain.Alignment setting). The code of the subroutine takes into consideration the specified alignment and prints the string on the page. To change the alignment of the text, set the Alignment property of the TextBox control to a different value and then run the program. For more information on left/right/center aligning a string across the page see last month's 10-Minute Solution.
The ClearScreen() subroutine clears the preview form in anticipation of the next page, or emits the current page and starts a new one on the printer. The subroutine's code distinguishes between printing and previewing by examining the PRN object. If the PRN object represents the preview form, the code displays a white rectangle that represents the area of the page within the margins (the useful printable area on the page). This rectangle extends from the upper left to the lower right corner of the printable band and it's drawn only on the preview form, and not on the printer. Notice that you can use the same statement to draw the appropriate rectangle regardless of the page's orientation, because it relies on the Printer object's Orientation property, which is already set .