472,110 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Printing Problem

i have an application that has a list of printers to print, using a class module. my problem is when you print many times on different printers. it only prints on the first printer you choose even though i chose different printers. in order to print to another printer i have to close the application and choose another printer. its like its setting the printer to default for the entire app to use and cant change unless restarting the application i cant resolve this issue and i wish someone can help me. here is the the code i used for the class module. i didnt make the code i just i copied it somewhere else.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Declare Function GetProfileString Lib "kernel32" _
  4.     Alias "GetProfileStringA" _
  5.     (ByVal lpAppName As String, _
  6.     ByVal lpKeyName As String, _
  7.     ByVal lpDefault As String, _
  8.     ByVal lpReturnedString As String, _
  9.     ByVal nSize As Long) As Long
  10.  
  11.  
  12. Private Declare Function WriteProfileString Lib "kernel32" _
  13.     Alias "WriteProfileStringA" _
  14.     (ByVal lpszSection As String, _
  15.     ByVal lpszKeyName As String, _
  16.     ByVal lpszString As String) As Long
  17.  
  18. Private Declare Function SendMessage Lib "user32" _
  19.     Alias "SendMessageA" _
  20.     (ByVal hWnd As Long, _
  21.     ByVal wMsg As Long, _
  22.     ByVal wParam As Long, _
  23.     lParam As String) As Long
  24.  
  25. Private Const HWND_BROADCAST = &HFFFF
  26.  
  27. Private Const WM_WININICHANGE = &H1A
  28.  
  29.  
  30. Private Type OSVERSIONINFO
  31.     dwOSVersionInfoSize As Long
  32.     dwMajorVersion As Long
  33.     dwMinorVersion As Long
  34.     dwBuildNumber As Long
  35.     dwPlatformId As Long
  36.     szCSDVersion As String * 128
  37. End Type
  38.  
  39. Private Declare Function GetVersionExA Lib "kernel32" _
  40.     (lpVersionInformation As OSVERSIONINFO) As Integer
  41.  
  42. Private Declare Function OpenPrinter Lib "winspool.drv" _
  43.     Alias "OpenPrinterA" _
  44.     (ByVal pPrinterName As String, _
  45.     phPrinter As Long, _
  46.     pDefault As PRINTER_DEFAULTS) As Long
  47.  
  48. Private Declare Function SetPrinter Lib "winspool.drv" _
  49.     Alias "SetPrinterA" _
  50.     (ByVal hPrinter As Long, _
  51.     ByVal Level As Long, _
  52.     pPrinter As Any, _
  53.     ByVal Command As Long) As Long
  54.  
  55. Private Declare Function GetPrinter Lib "winspool.drv" _
  56.     Alias "GetPrinterA" _
  57.     (ByVal hPrinter As Long, _
  58.     ByVal Level As Long, _
  59.     pPrinter As Any, _
  60.     ByVal cbBuf As Long, _
  61.     pcbNeeded As Long) As Long
  62.  
  63. Private Declare Function lstrcpy Lib "kernel32" _
  64.     Alias "lstrcpyA" _
  65.     (ByVal lpString1 As String, _
  66.     ByVal lpString2 As Any) As Long
  67.  
  68. Private Declare Function ClosePrinter Lib "winspool.drv" _
  69.     (ByVal hPrinter As Long) As Long
  70.  
  71. Private Declare Function GetLastError Lib "kernel32" () As Long
  72.  
  73. Private Const CCHDEVICENAME = 32
  74. Private Const CCHFORMNAME = 32
  75.  
  76. Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
  77. Private Const PRINTER_ACCESS_ADMINISTER = &H4
  78. Private Const PRINTER_ACCESS_USE = &H8
  79. Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)
  80.  
  81. Private Const PRINTER_ATTRIBUTE_DEFAULT = 4
  82.  
  83. Private Type DEVMODE
  84.     dmDeviceName As String * CCHDEVICENAME
  85.     dmSpecVersion As Integer
  86.     dmDriverVersion As Integer
  87.     dmSize As Integer
  88.     dmDriverExtra As Integer
  89.     dmFields As Long
  90.     dmOrientation As Integer
  91.     dmPaperSize As Integer
  92.     dmPaperLength As Integer
  93.     dmPaperWidth As Integer
  94.     dmScale As Integer
  95.     dmCopies As Integer
  96.     dmDefaultSource As Integer
  97.     dmPrintQuality As Integer
  98.     dmColor As Integer
  99.     dmDuplex As Integer
  100.     dmYResolution As Integer
  101.     dmTTOption As Integer
  102.     dmCollate As Integer
  103.     dmFormName As String * CCHFORMNAME
  104.     dmLogPixels As Integer
  105.     dmBitsPerPel As Long
  106.     dmPelsWidth As Long
  107.     dmPelsHeight As Long
  108.     dmDisplayFlags As Long
  109.     dmDisplayFrequency As Long
  110.     dmICMMethod As Long
  111.     dmICMIntent As Long
  112.     dmMediaType As Long
  113.     dmDitherType As Long
  114.     dmReserved1 As Long
  115.     dmReserved2 As Long
  116. End Type
  117.  
  118.  
  119. Private Type PRINTER_INFO_5
  120.     pPrinterName As String
  121.     pPortName As String
  122.     Attributes As Long
  123.     DeviceNotSelectedTimeout As Long
  124.     TransmissionRetryTimeout As Long
  125. End Type
  126.  
  127. Private Type PRINTER_DEFAULTS
  128.     pDatatype As Long
  129.     pDevMode As DEVMODE
  130.     DesiredAccess As Long
  131. End Type
  132.  
  133. Private m_sCurrPrinterDevName As String
  134. Private m_sPrevPrinterDevName As String
  135. Private m_sPrevPrinterDriver As String
  136. Private m_sPrevPrinterPort As String
  137.  
  138. Private Function PtrCtoVbString(Add As Long) As String
  139.     Dim sTemp As String * 512, x As Long
  140.  
  141.     x = lstrcpy(sTemp, Add)
  142.     If (InStr(1, sTemp, Chr(0)) = 0) Then
  143.          PtrCtoVbString = ""
  144.     Else
  145.          PtrCtoVbString = Left(sTemp, InStr(1, sTemp, Chr(0)) - 1)
  146.     End If
  147. End Function
  148.  
  149. Private Function SetDefaultPrinter(ByVal DeviceName As String, ByVal DriverName As String, ByVal PrinterPort As String) As Boolean
  150.     Dim DeviceLine As String
  151.     Dim r As Long
  152.     Dim l As Long
  153.  
  154.     DeviceLine = DeviceName & "," & DriverName & "," & PrinterPort
  155.     r = WriteProfileString("windows", "Device", DeviceLine)
  156.  
  157.     If r Then
  158.         l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
  159.         SetDefaultPrinter = True
  160.         m_sCurrPrinterDevName = DeviceName
  161.     Else
  162.         SetDefaultPrinter = False
  163.     End If
  164. End Function
  165.  
  166. Private Function Win95SetDefaultPrinter(ByRef DeviceName As String) As Boolean
  167.     Dim Handle As Long
  168.     Dim pd As PRINTER_DEFAULTS
  169.     Dim x As Long
  170.     Dim need As Long
  171.     Dim pi5 As PRINTER_INFO_5
  172.     Dim LastError As Long
  173.  
  174.     If DeviceName = "" Then
  175.         Win95SetDefaultPrinter = False
  176.         Exit Function
  177.     End If
  178.  
  179.     pd.pDatatype = 0&
  180.     pd.DesiredAccess = PRINTER_ALL_ACCESS
  181.  
  182.     x = OpenPrinter(DeviceName, Handle, pd)
  183.     If x = False Then
  184.         Win95SetDefaultPrinter = False
  185.         Exit Function
  186.     End If
  187.  
  188.     x = GetPrinter(Handle, 5, ByVal 0&, 0, need)
  189.     ReDim t((need \ 4)) As Long
  190.  
  191.     x = GetPrinter(Handle, 5, t(0), need, need)
  192.     If x = False Then
  193.         Win95SetDefaultPrinter = False
  194.         Exit Function
  195.     End If
  196.  
  197.     pi5.pPrinterName = PtrCtoVbString(t(0))
  198.     pi5.pPortName = PtrCtoVbString(t(1))
  199.     pi5.Attributes = t(2)
  200.     pi5.DeviceNotSelectedTimeout = t(3)
  201.     pi5.TransmissionRetryTimeout = t(4)
  202.  
  203.     pi5.Attributes = PRINTER_ATTRIBUTE_DEFAULT
  204.  
  205.     x = SetPrinter(Handle, 5, pi5, 0)
  206.     If x = False Then
  207.         Win95SetDefaultPrinter = False
  208.         Exit Function
  209.     End If
  210.  
  211.     Call ClosePrinter(Handle)
  212.     m_sCurrPrinterDevName = DeviceName
  213.     Win95SetDefaultPrinter = True
  214. End Function
  215.  
  216. Private Sub GetDriverAndPort(ByVal Buffer As String, ByRef DriverName As String, ByRef PrinterPort As String)
  217.     Dim iDriver As Integer
  218.     Dim iPort As Integer
  219.  
  220.     DriverName = ""
  221.     PrinterPort = ""
  222.  
  223.     iDriver = InStr(Buffer, ",")
  224.     If iDriver > 0 Then
  225.         DriverName = Left(Buffer, iDriver - 1)
  226.  
  227.         iPort = InStr(iDriver + 1, Buffer, ",")
  228.  
  229.         If iPort > 0 Then
  230.             PrinterPort = Mid(Buffer, iDriver + 1, iPort - iDriver - 1)
  231.         End If
  232.     End If
  233. End Sub
  234.  
  235. Private Function WinNTSetDefaultPrinter(ByRef DeviceName As String) As Boolean
  236.     Dim Buffer As String
  237.     Dim DriverName As String
  238.     Dim PrinterPort As String
  239.     Dim r As Long
  240.  
  241.     If DeviceName <> "" Then
  242.         Buffer = Space(1024)
  243.         r = GetProfileString("PrinterPorts", DeviceName, "", Buffer, Len(Buffer))
  244.  
  245.         Call GetDriverAndPort(Buffer, DriverName, PrinterPort)
  246.  
  247.         If DriverName <> "" And PrinterPort <> "" Then
  248.             WinNTSetDefaultPrinter = SetDefaultPrinter(DeviceName, DriverName, PrinterPort)
  249.         Else
  250.             WinNTSetDefaultPrinter = False
  251.         End If
  252.  
  253.     End If
  254.  
  255. End Function
  256.  
  257. Function SetPrinterAsDefault(ByVal DeviceName As String) As Boolean
  258.     Dim osinfo As OSVERSIONINFO
  259.     Dim retvalue As Integer
  260.  
  261.     osinfo.dwOSVersionInfoSize = 148
  262.     osinfo.szCSDVersion = Space$(128)
  263.     retvalue = GetVersionExA(osinfo)
  264.  
  265.     If m_sCurrPrinterDevName <> DeviceName Then
  266.         If osinfo.dwMajorVersion = 3 And osinfo.dwMinorVersion = 51 And osinfo.dwBuildNumber = 1057 And osinfo.dwPlatformId = 2 Then
  267.             SetPrinterAsDefault = WinNTSetDefaultPrinter(DeviceName)
  268.         ElseIf osinfo.dwMajorVersion = 4 And osinfo.dwPlatformId = 1 Then
  269.             SetPrinterAsDefault = Win95SetDefaultPrinter(DeviceName)
  270.         ElseIf osinfo.dwMajorVersion = 4 And osinfo.dwMinorVersion = 0 And osinfo.dwBuildNumber = 1381 And osinfo.dwPlatformId = 2 Then
  271.             SetPrinterAsDefault = WinNTSetDefaultPrinter(DeviceName)
  272.         ElseIf osinfo.dwMajorVersion > 4 Then
  273.             SetPrinterAsDefault = WinNTSetDefaultPrinter(DeviceName)
  274.         End If
  275.     Else
  276.         SetPrinterAsDefault = True
  277.     End If
  278.     End Function
  279.  
  280. Private Sub Class_Initialize()
  281.     Dim Buffer As String
  282.     Dim r As Long
  283.  
  284.     Buffer = Space(8192)
  285.     r = GetProfileString("windows", "Device", "", Buffer, Len(Buffer))
  286.     If r Then
  287.         Buffer = Mid(Buffer, 1, r)
  288.         m_sPrevPrinterDevName = Mid(Buffer, 1, InStr(Buffer, ",") - 1)
  289.         m_sPrevPrinterDriver = Mid(Buffer, InStr(Buffer, ",") + 1, InStrRev(Buffer, ",") - InStr(Buffer, ",") - 1)
  290.         m_sPrevPrinterPort = Mid(Buffer, InStrRev(Buffer, ",") + 1)
  291.     Else
  292.         m_sPrevPrinterDevName = ""
  293.         m_sPrevPrinterDriver = ""
  294.         m_sPrevPrinterDevName = ""
  295.     End If
  296.     m_sCurrPrinterDevName = m_sPrevPrinterDevName
  297.  
  298. End Sub
  299.  
  300. Private Sub Class_Terminate()
  301.     Call SetPrinterAsDefault(m_sPrevPrinterDevName)
  302. End Sub
  303.  
  304.  
Dec 1 '14 #1
0 953

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Rami | last post: by
5 posts views Thread by rd | last post: by
reply views Thread by Programatix | last post: by
2 posts views Thread by Sukh | last post: by
reply views Thread by Sukh | last post: by
1 post views Thread by benfly08 | last post: by
6 posts views Thread by babaidebnath | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.