Printer Details VBA

mini12

New Member
Joined
Aug 1, 2005
Messages
7
Hello,

I have a excel userform, where i use a combo box to select printers on a network, heres the code i am using:


Code:
'//Get the version of Windows OS
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
'Private Const VER_PLATFORM_WIN32s = 0 '//Change here in case - Win32s
'Private Const VER_PLATFORM_WIN32_WINDOWS = 1 '//Change here in case - Windows 95/98(?)
Private Const VER_PLATFORM_WIN32_NT = 2 '//Change here in case - Windows NT
Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long

Private Type PRINTER_INFO_1 '//Get Printer Information
flags As Long
pPDescription As Long
pName As Long
pComment As Long
End Type
Private Const PRINTER_ENUM_LOCAL = &H2
Private Const PRINTER_ENUM_CONNECTIONS = &H4 '
Private Declare Function EnumPrinters Lib "WINSPOOL.DRV" Alias "EnumPrintersA" _
(ByVal flags As Long, _
ByVal Name As String, _
ByVal Level As Long, _
pPrinterEnum As Any, _
ByVal cdBuf As Long, _
pcbNeeded As Long, _
pcReturned As Long) As Long
Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Source As Any, ByVal length&)

Public Function EnumPrinter() As Variant
Dim asPrinter() As String
enumPrinter_Engine PRINTER_ENUM_LOCAL, asPrinter
If isWindowsNT Then enumPrinter_Engine PRINTER_ENUM_CONNECTIONS, asPrinter
EnumPrinter = asPrinter
End Function

Public Sub enumPrinter_Engine(ByVal iiEnumType As Long, ByRef ioasPrinter() As String)
Dim abEnumBuffer() As Byte, cBufferSize As Long
Dim uPrinterInfo As PRINTER_INFO_1, cStructSize As Long
Dim lngPrinters As Long
Dim i As Long, lngStart As Long
Const lngLevel As Long = 1
Const lngPrinterMax As Long = 64
Call EnumPrinters(iiEnumType, vbNullString, lngLevel, ByVal 0&, 0, cBufferSize, lngPrinters)
If cBufferSize = 0 Then Exit Sub
ReDim abEnumBuffer(0 To cBufferSize - 1)
Call EnumPrinters(iiEnumType, vbNullString, lngLevel, abEnumBuffer(0), cBufferSize, cBufferSize, lngPrinters)
If lngPrinters = 0 Then Exit Sub
If CntArr(ioasPrinter()) = 0 Then
lngStart = 0
ReDim ioasPrinter(0 To lngPrinters - 1)
Else
lngStart = UBound(ioasPrinter) + 1
ReDim Preserve ioasPrinter(0 To lngStart + lngPrinters - 1)
End If
cStructSize = Len(uPrinterInfo)
For i = 0 To lngPrinters - 1
Call MoveMemory(uPrinterInfo, abEnumBuffer(cStructSize * i), cStructSize)
ioasPrinter(lngStart + i) = GetPrinterStrings(uPrinterInfo.pName, lngPrinterMax)
Next i
End Sub
Private Function GetPrinterStrings(ByVal ipString As Long, inBytes As Long) As String
ReDim abBuffer(0 To inBytes) As Byte
Call MoveMemory(abBuffer(0), ByVal ipString, inBytes)
GetPrinterStrings = StrConv(abBuffer(), vbUnicode)
GetPrinterStrings = Left$(GetPrinterStrings, InStr(GetPrinterStrings, vbNullChar) - 1)
End Function
Private Property Get isWindowsNT() As Boolean
Dim OsVers As OSVERSIONINFO
OsVers.dwOSVersionInfoSize = Len(OsVers)
Call GetVersionEx(OsVers)
isWindowsNT = (OsVers.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Property
Public Function CntArr(ByVal vntArr As Variant, Optional ByVal lngDimention As Long = 1) As Long
On Error Goto Terminate
CntArr = 0
CntArr = UBound(vntArr, lngDimention) - LBound(vntArr, lngDimention) + 1
Exit Function
Terminate:
Exit Function
End Function


Private Sub UserForm_Initialize()
Dim strsPrinterName As Variant
Me.Caption = "Please select printer"
With ComboBox1
For Each strsPrinterName In EnumPrinter()
.AddItem strsPrinterName
Next
End With
End Sub

But can get a printer details onto the userform when i select one from the combo box, like the way its done on excels print dialog. I need the printers name, description, status, Location, its jobs and any comments.

Thanks to any that helps

Mini12
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top