Intrinsic Ctrls
ActiveX Ctrls
Cmd Reference
File Handling

Drag and Drop
Error Handling
Database Controls

Internet File Transfers
Perl Commands
Web Page Forms

GBIC >> VB >> Tutorials >> Printing
Let's amend the computer program "model" which I gave in the tutorial section on file handling. A computer program basically consists of a user who enters data, a file in which the data (or calculated results) are stored, and a paper copy of the data. The printed copy is usually formatted in such a way as to allow the user to make sense of the data. The point of this short essay is that most users expect to be able to get a hard copy (meaning a paper copy) of their data. They want it stored, but they also want to be able to take the results away from the computer for subsequent review. Hence - printing! An important fact is that how well the data is presented to the user can make or break the success of the application. Learning to use the printing tools of VB is critical to your success as a programmer.


For those of you who remember BASIC printing, the news with Visual Basic is both (mostly) good and (not much) bad. With BASIC and before the advent of the proportional fonts of Windows it was easier to figure out where your print statements were going to put the output. Since every character took up the same width, you only had to count positions (80 per line) as part of figuring out how to code print statements.

The bad news with DOS BASIC was that you had to print one line at a time. You couldn't print line 40, then go back to line 10. The really good news is that in Visual Basic, an object known as the printer is available to you. The printer is like a work space to which you can direct the output of print statements or the output of graphics statements. You can send text/graphics output to the printer object in any order you want, then when you're done you can tell Visual Basic to print the material to a physical printer.

The other good news about Visual Basic is that you can also "print" inside an applications window (a form or a picture control) and Windows will handle the movement of the printed output as you move windows around or as you move other windows in front of the printed material. With DOS BASIC you had to handle all of that yourself!

The (almost) bad news with Visual Basic printing is really a Windows issue. Although there are non-proportional fonts (same width for each letter) in Windows, they really don't look professional. So most folks program the print statements to use proportional (different width for each letter, to enhance to display of the text) fonts. The downside to this is that since each character is not the same width, you have to specify the X/Y coordinates of the print location.

VB has some tools to make it a little easier than I make it sound, and the conveniences of the printer object really do outweigh the inconvenience of handling proporational fonts.

Printing Surfaces

Ok, I've mentioned that in VB you can "print" to more than just a physical printer. In fact, you can print to a form, to a picture control, to the DEBUG object (more on this in another section) and also to any physical printer connected to your PC or to any shared network printer to which your PC is connected.

Here's one of your first opportunities to see methods at work. Do you remember we described methods as being commands within VB which act on a control? Well, printing (by which I mean the output of text as well as the output of graphics) is a method that you use with an object. Here are three examples:

form1.print "test"
picture1.print "test"
printer.print "test"

All three of these do the same basic thing, which is to print the word "test" to an object. The first two print on-screen to a form (form1) and to a picture control (picture1). The last prints to the printer object that I mentioned earlier.

In every case where a print method is used, the printing takes place at the current X/Y position of the object. There are several ways by which VB determines the current X/Y position of an object (this isn't the same as the .Top or .Left properties of an object, which determine the positioning of a window on your screen). However, you can always override VB by using code to set the current X/Y position. Here's the code for it:

form1.currentx = 25
picture1.currenty = 200
printer.currentx = 844

Notice that there are two properites .CurrentX and .CurrentY. You can set them using the statements I just gave, or you can use them to return the current X/Y position as follows:

i = form1.currentX
j = form1.currentY

One complicating factor in VB that you'll just have to get used to is the concept of an object's scale. You can set the scale of an object to pixels, twips, inches, centimeters, points, character, and millimeters. The default value is twips (1440 twips is an inch). Depending on the application, you can make a big difference in the code if you select a scale to match the type of application you're writing.

For example, when you're doing graphics work it may be much better to deal in pixels. In general, however, I've found that only the pixel, twips, and inches scales are necessary to meet my own programming needs.

One final comment about the difference in printing on forms and picture controls versus printer on the printer. With forms/controls, the printing takes place immediately. With the printer, none of the information is sent to the printer until you use the .EndDoc method, which tells VB you're done printing and to send all of the information on the the physical printer.

I go into more detail about printing graphics in the next section of the tutorial.

Printing Tips

Now that you have some general ideas of how printing works, lets do some examples. I've picked some of the examples because they provide tips on how to do some of the most typical printing tasks you're likely to need.

Print at a specific location
To print at a specific location, such as 1 inch down and 4 inches over, you must set the currentx and currenty properties accordingly. If you'll first set the scalemode to inches you won't have to do any conversions.

printer.scalemode = vbinches
printer.currentx = 4.0
printer.currenty =1.0

Print multiple lines
Each subsequent time you execute a print statement, VB resets the X/Y coordinates to a position corresponding to the "next line down". In other words, it moves the current position to the far left and one line down. You can override this if you choose, but here's a simple printing of four lines one after the other:

printer.print "line 1"
printer.print "line 2"
printer.print "line 3"
printer.print "line 4"

Each line will print at the left side of the printer, and the printout will be four lines deep.

Print in the middle of a line
To printer in the middle of a line, you'll need to figure out the width of the text you're about to print, then begin printing 1/2 that difference between that width and the width of the printer object. This involves the use of the method textwidth, which gives the length (in the current scalemode) of a text string.

printer.currentx = (printer.width - printer.textwidth("print string")) / 2 printer.print "print string"

Print in the middle of a page (top to bottom)
This case is just like the one before, except you also position halfway between the top and bottom of the page an make use of the textheight method.

printer.currentx = (printer.width - printer.textwidth("print string")) / 2
printer.currenty = (printer.height - printer.textheight("print string")) / 2
printer.print "print string"

Print in columns
If you want to print starting in a column, then reposition currentx for each item to be printed. This example prints at 1, 3, and 5 inches.

printer.scalemode = vbinches
printer.currentx = 1
printer.print "column1 data";
printer.currentx = 3
printer.print "column2 data";
printer.currentx = 5

Notice that the print statement ends with a ";", which directs VB to leave the currentx/currenty values positioned at the location following the current print operation.

Print as right justified
Left justification is the normal method automatically happens with justification taking place at the current value of .CurrentX. For right justification, you want to start printing at a position so that the length of the text will result in an end-of-print action at justification position.

printer.currentx = rightjustificationposition - printer.textwidth("print text")
printer.print "print text"

Summary Hopefully that examples give you an idea of the basic concepts of printing. Head over to my control summary chart - available in Excel 97 and Excel 5.0 formats - to see a complete listing of the properties and methods available to you.

Finally, just in case you don't go looking right away, please remember that you can set the orientation of the printer with the .orientation property. This lets you switch between landscape and portrait very easily within your code.