Select Printer From List

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
AndreRet AndreRet is offline Offline Oct 27th, 2009, 3:09 pm |
0
Let you choose between all printers available on your pc or network. Download zip and enjoy. Royal free.
Attached Files
File Type: zip Printer Select.zip (4.4 KB, 21 views)
Quick reply to this message  
Visual Basic 4 / 5 / 6 Syntax
  1. Option Explicit
  2. Private 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
  3.  
  4. Private Function GetDefaultPrinter() As String
  5. Dim strBuffer As String * 254
  6. Dim iRetValue As Long
  7. Dim strDefaultPrinterInfo As String
  8. Dim tblDefaultPrinterInfo() As String
  9.  
  10. ' Retreive current default printer information
  11. iRetValue = GetProfileString("windows", "device", ",,,", strBuffer, 254)
  12. strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
  13. tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, ",")
  14. GetDefaultPrinter = tblDefaultPrinterInfo(0)
  15. End Function
  16.  
  17. Private Sub AddToPrinterList(LBox As ListView, ByRef Counter As Integer, Col1 As String, DefaultPrinter As Boolean)
  18. 'add the printers to the list view control
  19. 'add one to the printer number for the index since the control does not like zero and then
  20. 'remove again when you want to assign the printer
  21. With LBox
  22. .ListItems.Add Counter + 1, Col1 & Counter, Col1, 2
  23. If DefaultPrinter Then
  24. .ListItems(Counter + 1).Selected = True
  25. End If
  26. End With
  27. End Sub
  28.  
  29. Private Sub cmdCancel_Click()
  30. Unload Me
  31. End Sub
  32.  
  33. Private Sub cmdPrint_Click()
  34. 'set the printer and print something
  35. If Len(PrinterList.SelectedItem.Text) > 0 Then
  36. Set Printer = Printers(PrinterList.SelectedItem.Index - 1)
  37. Printer.Copies = NoCopies
  38. End If
  39. 'add any print statements here that you want
  40. Printer.Print "hello"
  41. Printer.EndDoc
  42. cmdCancel_Click
  43. End Sub
  44.  
  45. Private Sub Form_Load()
  46. Dim Counter As Integer
  47. Dim defPrinter As String
  48.  
  49. 'enumerate the printers collection
  50. If Printers.Count > 0 Then
  51. defPrinter = GetDefaultPrinter
  52. For Counter = 0 To Printers.Count - 1
  53. AddToPrinterList PrinterList, Counter, Printers(Counter).DeviceName, Printers(Counter).DeviceName = defPrinter
  54. Next
  55. Else
  56. MsgBox "No Printers Are Installed!" & vbCrLf & vbCrLf & _
  57. "You must install at least one printer before you can print!", vbCritical, "No Printer Available!"
  58. Unload Me
  59. End If
  60. End Sub
  61.  
  62. Private Sub VScroll1_Change()
  63. 'use a scroll bar to increment the number of copies
  64. 'can add loads of other printing options as you see fit
  65. NoCopies = Abs(VScroll1)
  66. End Sub

Message:


Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC