Advertisement
KySoto

Set Printer By Paper Dimensions

Oct 22nd, 2019
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.   Public Enum DeviceCapabilitiesFlags
  5.     DC_FIELDS = 1
  6.     DC_PAPERS = 2
  7.     DC_PAPERSIZE = 3
  8.     DC_MINEXTENT = 4
  9.     DC_MAXEXTENT = 5
  10.     DC_BINS = 6
  11.     DC_DUPLEX = 7
  12.     DC_SIZE = 8
  13.     DC_EXTRA = 9
  14.     DC_VERSION = 10
  15.     DC_DRIVER = 11
  16.     DC_BINNAMES = 12
  17.     DC_ENUMRESOLUTIONS = 13
  18.     DC_FILEDEPENDENCIES = 14
  19.     DC_TRUETYPE = 15
  20.     DC_PAPERNAMES = 16
  21.     DC_ORIENTATION = 17
  22.     DC_COPIES = 18
  23.     DC_BINADJUST = 19
  24.     DC_EMF_COMPLIANT = 20
  25.     DC_DATATYPE_PRODUCED = 21
  26.     DC_COLLATE = 22
  27.     DC_MANUFACTURER = 23
  28.     DC_MODEL = 24
  29.     DC_PERSONALITY = 25
  30.     DC_PRINTRATE = 26
  31.     DC_PRINTRATEUNIT = 27
  32.     DC_PRINTERMEM = 28
  33.     DC_MEDIAREADY = 29
  34.     DC_STAPLE = 30
  35.     DC_PRINTRATEPPM = 31
  36.     DC_COLORDEVICE = 32
  37.     DC_NUP = 33
  38.     DC_MEDIATYPENAMES = 34
  39.     DC_MEDIATYPES = 35
  40. End Enum
  41. Public Enum LabelType
  42. lt8_5x11 = 0
  43. lt3x2 = 1
  44. lt1_5x1 = 2
  45. End Enum
  46.  
  47. Public Type POINT
  48.     x As Long
  49.     y As Long
  50. End Type
  51.  
  52. Public Declare Function DeviceCapabilities _
  53.   Lib "winspool.drv" _
  54.     Alias "DeviceCapabilitiesA" _
  55.       (ByVal lpDeviceName As String, _
  56.        ByVal lpPort As String, _
  57.        ByVal iIndex As Long, _
  58.        ByRef lpOutput As Any, _
  59.        ByRef lpDevMode As Any) _
  60.     As Long
  61.  
  62. Public Declare Function StrLen _
  63.   Lib "kernel32.dll" _
  64.     Alias "lstrlenA" _
  65.       (ByVal lpString As String) _
  66.     As Long
  67.  
  68. Public Function GetPrinterNameByPaperDimensions(ByRef argIn As LabelType, Optional ByVal argColor As Boolean) As String
  69.     Dim defaultPrinter() As String
  70.     Dim paperCount As Long
  71.     Dim NameArray() As Byte
  72.     Dim i As Long
  73.     Dim paperNames() As String
  74.     Dim paperName As String
  75.     Dim ctr As Long
  76.     Dim AllNames As Variant
  77.     Dim p As Printer
  78.     Dim PIn As POINT
  79.     Dim out As String
  80.     out = ""
  81.     PIn = GetPaperXY(argIn)
  82.     If Not (PIn.x = 0 And PIn.y = 0) Then
  83.         For Each p In Application.Printers
  84.             ctr = 0
  85.             If Not (p.DeviceName Like "*eprint*" Or p.DeviceName Like "*oneNote*" Or p.DeviceName Like "*xps*" Or p.DeviceName Like "*fax*" Or p.DeviceName Like "*pdf*") Then
  86.                 'defaultPrinter = Split(Application.Printer, " on ")
  87.                paperCount = DeviceCapabilities(p.DeviceName, p.Port, DC_PAPERSIZE, ByVal 0&, ByVal 0&)
  88.                 ReDim paperNames(1 To paperCount)
  89.                 ReDim NameArray(0 To paperCount * 64) As Byte
  90.                 ' Get paper names
  91.                paperCount = DeviceCapabilities(p.DeviceName, p.Port, DC_PAPERNAMES, NameArray(0), 0)
  92.                 'convert the retrieved byte array to an ANSI string
  93.                AllNames = StrConv(NameArray, vbUnicode)
  94.                 'ReDim PaperSizes(1 To paperCount)
  95.                ReDim paperNames(1 To paperCount)
  96.                 'loop through the string and search for the names of the papers
  97.                For i = 1 To Len(AllNames) Step 64
  98.                     ctr = ctr + 1
  99.                     paperName = Mid(AllNames, i, 64)
  100.                     paperName = Left(paperName, StrLen(paperName))
  101.                     If paperName <> vbNullString Then
  102.                         paperNames(ctr) = paperName
  103.                     End If
  104.                 Next i
  105.                 ReDim papersizes(1 To paperCount) As POINT
  106.                 paperCount = DeviceCapabilities(p.DeviceName, p.Port, DC_PAPERSIZE, papersizes(1), 0)
  107.                 For i = 1 To paperCount
  108.                     If papersizes(i).x = PIn.x And papersizes(i).y = PIn.y Then
  109.                         out = p.DeviceName
  110.                         Exit For
  111.                     End If
  112.                 Next
  113.             End If
  114.         Next
  115.     End If
  116.     GetPrinterNameByPaperDimensions = out
  117. End Function
  118.  
  119. Public Function GetPaperXY(argIn As LabelType) As POINT
  120.     'dimensions are in 10ths of a milimeter
  121.    'lt8_5x11 = 0
  122.    'lt3x2 = 1
  123.    'lt1_5x1 = 2
  124.    Dim p As POINT
  125.     p.x = 0
  126.     p.y = 0
  127.     'cant just store the point in the dictionary since it wants a class. this seems to be a good compramise.
  128.    Const conversionFactor As Long = 254
  129.     Static x As Object
  130.     Static y As Object
  131.     If x Is Nothing Then
  132.         Set x = CreateObject("Scripting.Dictionary")
  133.         x.add lt8_5x11, 8.5 * conversionFactor
  134.         x.add lt3x2, 3 * conversionFactor
  135.         x.add lt1_5x1, 1.5 * conversionFactor
  136.     End If
  137.     If y Is Nothing Then
  138.         Set y = CreateObject("Scripting.Dictionary")
  139.         y.add lt8_5x11, 11 * conversionFactor
  140.         y.add lt3x2, 2 * conversionFactor
  141.         y.add lt1_5x1, 1 * conversionFactor
  142.     End If
  143.     p.x = x(argIn)
  144.     p.y = y(argIn)
  145.     GetPaperXY = p
  146. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement