Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrint multiline text at a specific location on a form or printer
Keywordsprint, multiline, preview
CategoriesGraphics
 
If you print multiline text using the Print method, Visual Basic resets the X coordinate to the object's left margin every time it encounters a new line. The result looks something like this:

    Line 1
Line 2
Line 3

The PrintMultiline subroutine splits the text into lines and prints each line starting at the same X coordinate to give a result more like this:

    Line 1
    Line 2
    Line 3
 
' Print multi-line text on an object
' at the indicated coordinates.
Public Sub PrintMultiline(ByVal obj As Object, ByVal txt As _
    String, ByVal X As Single, ByVal Y As Single)
Dim text_lines As Variant
Dim i As Integer

    ' Split the text into lines.
    text_lines = Split(txt, vbCrLf)

    ' Print the lines.
    obj.CurrentY = Y
    For i = LBound(text_lines) To UBound(text_lines)
        obj.CurrentX = X
        obj.Print text_lines(i)
    Next i
End Sub
 
John Piel had this to add:

The code crashed on my W2K system. The code in Q154078 worked great on my Epson Stylus Color ink jet. The code looks a lot like yours, however.

Q175083 claims that an ink jet or laser printer will not work in single line mode. This is wrong. Adding the "Generic/Text Only" printer and setting it to use the same port as your ink jet/laser should work with most "page" printers. This is how I got my ink jet to work.

For more information, go to:

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated