Browse DevX
Sign up for e-mail newsletters from DevX


Expand Your VB6 Printing Repertoire—Part I-4 : Page 4

Printout Preview
VB6 has no tools to automatically generate previews, but you can emulate the printed page on a form quite easily, because both the Form object and the Printer object provide the same methods for generating graphics elements. Conveniently, that means you can use the same statements to create identical images on either an on-screen form or on a printed page. The printout on the form won't be absolutely accurate though, because you can only emulate the printer's behavior; the output doesn't pass through the printer's driver, so it's not completely accurate. I'll show you how to build tools to emulate the printer's output on a form, but the preview may not always be identical to the actual printout. You can use these tools in your projects—and you're welcome to improve them—but be aware of the limitations. You can preview a printout to verify that the output "looks" correct (see that the various elements are placed correctly on the page and that no text leaks into the margins), but don't be surprised if the actual printout differs slightly, perhaps fitting an additional line on the printout page, as opposed to the preview form.

To simplify the switching between the Form preview object and the Printer object at run time, you can create an object variable and set it to the appropriate object from within your code. For example, to set the object to a form, you could write:

   Dim PRN As Object
   Set PRN = Form1
Alternatively, you can set the PRN object to the Printer:

   Set PRN = Printer
After that, you can create graphics elements by calling the appropriate methods on the PRN object, such as PRN.Print, PRN.Line, and so on. Of course, you give up the convenience of early binding, but you gain the advantage of having the same code work with both the Form and the Printer objects. Unfortunately, you can also write code such as PRN.BackColor, even if the PRN object refers to the Printer, which doesn't provide a BackColor property, and thus would cause a run-time error. One compromise is to work with the Form object while writing and testing your code, and introduce the PRN object during the last stages of development. You should also avoid calling methods or properties of the Form object that are not supported by the Printer object. Of course, another way is to duplicate the code for both objects, and use early binding to catch those errors at compile time.

Thanks for your registration, follow us on our social networks to keep up-to-date