Technology Microsoft Software & solutions

VB6 Compatible Printing

I get some of my best ideas from email. Poorahmadi wrote with this simple question: "Can I use 'Print 7 * 8, 6 / 2' with Visual Basic 2010?"

This question really goes way back to a day when printers were connected directly to a computer port. Code like that usually doesn't work in today's networked world. Microsoft recommends using the PrintDocument class instead and there is extensive documentation at the Microsoft site available about that class.


Still, I wasn't sure about the Print command. So I started looking for a more complete answer. This started me into a journey through Microsoft's documentation.

My initial step was to simply give it a try. This made it clear that the initial answer to Poorahmadi is, "Not without changing it."

If you code a Print statement in an otherwise default VB.NET program, you get the Microsoft.VisualBasic FileSystem Print object. That one requires a FileNumber parameter and the compiler interprets the statment as, "Print 3 using FileNumber 56" and crashes on an IOException.

But it turns out that VB.NET supports at least two Print objects. Another one, in the Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6 namespace (Whew! These namespace paths are getting out of hand!) will support a Print without a FileNumber ... if you code it right. Add an Imports statement for the namespace and then declare a New Printer object:

Dim p As New Printer
Then you can code ...

p.Print(7 * 8, 6 / 2)p.EndDoc()

... and it works! My networked default printer wasted a sheet of paper showing me a tiny "56 3" in the upper left corner. (Sorry, trees!) The documentation for this method at Microsoft mainly discusses how to upgrade to .NET and how to install these upgrades. (Something Microsoft calls "bootstrapping".) There is very little about about actual details that might be useful to someone who doesn't want to upgrade.

The Microsoft.VisualBasic FileSystem version of the Print object requires entirely different syntax:

FileOpen(1, "C:\test.txt", OpenMode.Output)Print(1, CStr(7 * 8) & "" & CStr(6 / 2))
This uses the default FileSystem Print and because it sends the output to a file, it doesn't print immediately. The "test.txt" file can be sent to a printer later. In the past, it was possible to use a Copy command to send a file to a printer but the only code I can make work relies on a second program like Notepad. For example, this works:

FileOpen(1, "C:\Users\Milovan\Documents\test.txt", OpenMode.Output)Print(1, CStr(7 * 8) & "" & CStr(6 / 2))FileClose(1)Dim theCmd As String ="Notepad /P C:\Users\Milovan\Documents\test.txt"Shell(theCmd)
If anyone knows how to code a Copy command that will send a file directly to the printer with current versions of Windows, let us know. Although Notepad works, it also prints a header and footer that you might not want. If something like this is absolutely necessary in your program, most people download a third party printing utility to do the job.

Microsoft could improve their documentation by providing some information in the pages for each of the two Print statements about the fact that there is a different Print statement in a different namespace that works entirely differently. The documentation that exists is, yet again, another illustration of the fact that Microsoft is becoming so big that not only does the left hand not know what the right hand is doing, in many cases it seems that the left hand doesn't even know that a right hand exists.

Related posts "Technology : Microsoft Software & solutions"

How Do I Get Past the Main User Account Password on Windows XP?

Microsoft

How to Install the Windows XP on a Dell Latitude D600

Microsoft

How to Remove Enfocus PitStop

Microsoft

How to Uninstall MS Defender Vista

Microsoft

How to Upgrade From DirectX 9 to DirectX 10

Microsoft

How to Disable a Windows Update Virus

Microsoft

What Is Error 1720?

Microsoft

How to Cut Window Trim With a Miter Saw

Microsoft

How to Show the Grid on the MIDI Region in Protools

Microsoft

Leave a Comment