Using Ax

ucdev AxInsidePrinter. www.ucdev.com

Make whatever you want with your printers.

This Dll was developed to bring the developer an easy way to access low-level printer properties.

This is the properties list:

  • PrintersCount: Gets the total number of printers.

  • GetPrinterName: Gets the name of the printer based on his index.

  • GetDefPrinter: Gets the system default printer.

  • GetDuplex: Returns if printer duplex property is activated.

  • SetDuplex: Activate or Deactivate duplex printing.

  • GetPrinterJobs: Gets Job names from the selected printer.

  • GetDocExists: Looks if a specified job exists.

  • SetDefPrinter: Sets wich printer will be system default printer.

  • KillDoc: Deletes a job from the printer queue.

  • SetPauseJob: Pause/Unpause a Job.

  • GetPrinterTrays: Gets the trays of the specified printer.

  • SetPrinterTray: Sets default Tray for a printer.

How can I use AxInsidePrinter...

www.ucdev.com


Using AxInsidePrinter

AxInsidePrinter is a ActiveX Dll and needs to work an ActiveX container like Visual Basic, VC++ and so on. All the examples on this chapter will use Visual Basic 6 as the container.

This component comes with a Visual Basic 6 demo proyect. With this demo proyect we'll see AxInsidePrinter functionallity.

Declaring a new AxInsidePrinter Object:

To work with the component you may declare it on a way like this:

Dim axdll As AxInsidePrinter.clsPrinter
Set axdll = New AxInsidePrinter.clsPrinter

PrintersCount: Returns a String with total of printers in the system.

Function PrintersCount() As Integer

strTmp = axdll.PrintersCount

GetPrinterName: Returns the name of a printer, from his index.

Function GetPrinterName(ByVal intPrinterIndex As Integer) As String

intPrinterIndex: Index of the printer you'll get the name.

For example: I have two printers installed on my system, the first is a HP LaserJet 5Si, the another is a Kiocera FS-9000. I want to manipulate my 5Si, I'm not sure which is his EXACT name, so I'll obtain it.

You must use the classical functions to discard.

For x = 0 To dll.PrintersCount
If InStr(1, dll.Getprintername(x), "5si", vbTextCompare) Then
MsgBox dll.Getprintername(x) & " it's my fave printer"
End If
Next

This could work.The criterium to obtain the index and his name must be set for you.

GetDefPrinter: Returns a String with the name of the system default printer.

Function GetDefPrinter() As String

strTmp = axdll.GetDefPrinter

msgbox strTmp & " Is my default printer"

GetDuplex: To obtain duplex state of specified printer.

Function GetDuplex([ByVal strPrinterName As String]) As Integer

strPrinterName: Name of the printer to retrieve his duplex state. Optional, if empty will use the default printer.

Returns. 1- Duplex Disabled; 2- Duplex in Horizontal Orientation. 3- Vertical. Could return 5000 or 5001 if an access to printer error occurs.

intTmp = dll.GetDuplex(dll.GetPrinterName(0))
If intTmp = 1 Then
MsgBox "Duplex Disabled"
ElseIf intTmp = 2 Then
MsgBox "Duplex Horizontal"
ElseIf intTmp = 3 Then
MsgBox "Duplex Vertical"
End If

SetDuplex: Sets Duplex state.

Function SetDuplex(byval lngDuplexNewState As Long, Optional ByVal strPrinterName As String) As Boolean

strPrinterName: Name of the printer to set the new duplex state. Optional, if empty will use the Default Printer.

lsgDuplexNewState: You could use 1 to disable the duplex, 2 to use Horizontal, 3 to Vertical.

Returns True if the operation was complete, else returns false.

GetPrinterJobs: Gets Job names from the selected printer.

Function GetPrinterJobs([ByVal strPrinterName As String]) As String()

strPrinterName: Name of the printer to set the new duplex state. Optional, if empty will use the Default Printer.

Returns an array with all the docs in the queue of the selected printer.

dim arrTmp() as string

arrTmp = dll.GetPrinterJobs()

On Error GoTo ErrHndlr
For x = 0 To UBound(arrTmp)
Debug.Print arrTmp(x)
Next

ErrHndlr:
'If no jobs in queue UBound(arrTmp) returns error 9.
debug.print "no jobs"

GetDocExists: Looks if a specified job exists.

Function GetDocExists(ByVal strdocname As String, ByVal blnExact As Boolean, [ByVal strPrinterName As String]) As Boolean

strdocname: Name of the document to find.

blnExact: If True will look for the Exact name, if False will look strdocname string in the document name.

strPrinterName: Name of the printer to be used. Optional, if empty will use the Default Printer.

Returns True if the document exists, else returns false.

For example if I have in my queue a doc called "Testing".

blnTmp = dll.GetDocExists("Test", False)

With the second parameter to False, will look for "Test" in the string "Testing" and the string exists, so will return True.

blnTmp = dll.GetDocExists("Test", True)

With the second parameter to True, will look for the exact word "Test" the string don't exists, so will return False.

SetDefPrinter: Sets wich printer will be system default printer.

Sub SetDefPrinter(ByVal strPrinterName As String)

strPrinterName: Name of the printer to be used.

KillDoc: Deletes a job from the printer queue.

Function KillDoc(ByVal strdocname As String, [ByVal strPrinterName As String]) As Boolean

strDocname: Name of the document to kill.

strPrinterName: Name of the printer to be used. Optional, if empty will use the Default Printer.

Returns True if kill completed.

blnTmp = dll.KillDoc(txtJob, dll.GetDefPrinter)

SetPauseJob: Pause/Unpause a Job.

Function SetPauseJob(ByVal blnpaused As Boolean, ByVal strdocname As String, Optional ByVal strprintername As String) As Boolean

blnPaused: True to Pause the Job, False to Unpause the Job.

strDocName: Name of the document to pause. Can be only a partial string contained in the document name, see GetDocExists for more details.

strPrinterName: Name of the printer to be used. Optional, if empty will use the Default Printer.

strJobs = dll.GetPrinterJobs

If IsNumeric(UBound(strJobs)) Then
If blnPaused = True Then
dll.SetPauseJob False, strJobs(0)
blnPaused = False
Else
dll.SetPauseJob True, strJobs(0)
blnPaused = True
End If
End If

GetPrinterTrays: Gets the trays of the specified printer.

Function GetPrinterTrays([strPrinterName As String]) As String()

strPrinterName: Name of the printer. Optional, if empty will use the Default Printer.

Returns an array with all the Trays of the selected printer.

Dim arrTmp() As String
arrTmp() = dll.GetPrinterTrays()

SetPrinterTray: Sets default Tray for a printer.

Sub SetPrinterTray(lngTrayNumber As Long, [strPrinterName As String])

lngTrayNumber: Number of Tray, usually one element from the array returned from GetPrinterTrays.

strPrinterName: Name of the printer. Optional, if empty will use the Default Printer.

Dim arrTmp() As String, lngTray as string
arrTmp() = dll.GetPrinterTrays()
intTmp = InStr(1, arrTmp(1), " ")
lngTray = Left(arrTmp(1), intTmp - 1)
dll.SetPrinterTray lngTray

See Demo Project for detailed info.

www.ucdev.com


Distribution issues

To distribute your own programs with AxInsidePrinter inside you need to insert on your packages two files:

-AxInsidePrinter.dll : It's the heart of the program.

-Apigid32.dll : Third party dll, you'll need to distribute with AxInsidePrinter.

Both files are installed on the directory you installed AxInsidePrinter. When you distribute your program, remember this two files must be on the windows path. Preferably on System or System32 directory.