Listing all printers with name, Port in VB6 is simple as listing default printer port using Kernel32 library . Today we going show you how to do this.
As usual we start with function declaration, then define a new function which add all information into a list box. Here we goes
Kernel32 GetProfileString method
Declare Function GetProfileString Lib “kernel32.dll” Alias “GetProfileStringA” (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Add printer information into a list box
Public Sub GetPrinterList(lstPrinter As ListBox)Dim PrintData As PrinterDim defprinterpos%
For Each PrintData In Printers
‘ Add printer name and port to list
lstPrinter.AddItem PrintData.DeviceName & ” at: ” & PrintData.Port
‘ Check for default printer
If PrintData.DeviceName = Printer.DeviceName Then defprinterpos = lstPrinter.NewIndex
Next
lstPrinter.ListIndex = defprinterpos%
End Sub
This sub procedure will take list box as argument, and add printer to this list box.
Call the sub
The final line of code just invoke the procedure and see what we have.
Happy coding