We would like to thank all our subscribers for supporting us for the last 100 issues of Code of the Week. We hope you stay with us for the next 100 and more!
Want to get up to speed on the latest Visual Basic programming? Check out our training programs at http://www.codeoftheweek.com/vbtraining.html
If you would like to get paid for surfing the web, jump to http://www.codeoftheweek.com/paidsurf.html
This issue introduces a class module which shows how to change the printer Visual Basic currently prints its output to and provide a way to easily change the document name which shows in the printer queue.
If you have any questions about using this class, let us know at email@example.com
Public Property Let DocumentName(sDocName As String)
Sets the document name to appear in the print queue under Windows. By default this is the Project Name.
Public Property Get DocumentName() As String
Retrieves the document name to appear in the print queue under Windows. By default this is the Project Name.
Public Function IsPrinterReady() As Boolean
Determines if the printer is ready to print to. This is not 100% accurate, but it was the best we could come up with. If anyone has a more reliable way to determine this, please let us know and we will update this class.
Public Sub LoadPrinterCombo(cmb As ComboBox)
Load all the installed printer names into a combo box.
Public Sub SetPrinterByName(sName As String)
Sets the current Printer object to the printer name specified by sName. If the printer name is not found it will raise an error.
Public Sub StartJob() Public Sub EndJob()
If you want to use the DocumentName feature of this class you will need to call the StartJob method before sending anything to the printer. When you are through with the print job, call EndJob. EndJob will also perform the EndDoc so you will not need to call it yourself. By using these methods this printer class will manage the DocumentName correctly.
See above descriptions.
This sample will shows a sample printer selection form. It assumed you have added a combo box called cmbPrinters and a command button called cmdPrint and a textbox called txtDocName (which contains the name of the document you are printing).
Dim ptr As cPrinters Private Sub cmbPrinter_Click() ptr.SetPrinterByName cmbPrinter.Text End Sub Private Sub cmdPrint_Click() If ptr.IsPrinterReady Then ptr.DocumentName = txtDocName.Text ptr.StartJob On Error GoTo Handler Printer.Print "test printout1." ' print more stuff here. ptr.EndJob MsgBox "Print job has completed." Else MsgBox "Could not print to " & cmbPrinter.Text End If Exit Sub Handler: MsgBox "An error occurred while trying to print the document. The document will be removed from the print queue." ptr.EndJob End Sub Private Sub Form_Load() Set ptr = New cPrinters ptr.LoadPrinterCombo cmbPrinter End Sub
To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0100.html