473,246 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 999

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

Similar topics

0
by: Rami | last post by:
Hello All, We have a java application including applets, servlets and ejbs. Users can get reports and print them directly from applets. Our print methods uses PrinterJob and Graphics2d, imports...
5
by: rd | last post by:
Has anyone else noticed a problem when printing html with fieldsets in them? Whenever I print even the simplest document with a fieldset, the amount of data sent to the printer jumps from a few K...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
1
by: DCraig | last post by:
I'm having problems printing to a line printer from both Crystal Reports and SQL Server reporting services using dotnet. When I try and print a report from an application with Crystal I get the...
2
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the...
0
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the...
1
by: benfly08 | last post by:
Hi, I got a printing problem for my C# program. I need to pull out records from database and format them and then print them out. I used PrintDocument class and PrintPageEventHandler function...
1
monirul arfin
by: monirul arfin | last post by:
Hi all, can i ask a printing problem in this forum. monirul
0
by: GiftX | last post by:
VB 6.0 And Spreadsheet - Printing problem Good morning West. After much research, I found this code. It scans the spreadsheet and returns up to the last used cell. ...
6
by: babaidebnath | last post by:
Hello all, I am presently having a problem with printing in C#. The problem is my customer provided me a pre printed paper and I need to print value into some specific positions. But problem is C#...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.