PDA

View Full Version : Printer Details VBA



mini12
08-23-2005, 02:11 PM
Hello,

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

'//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

Bob Phillips
08-23-2005, 02:22 PM
Why do you need to emulate built-in functions, why not just use the various dialogs?

mini12
08-23-2005, 02:27 PM
What you mean???

Killian
08-23-2005, 04:01 PM
I reckon he means thisApplication.Dialogs(xlDialogPrint).Show