Find a list entry in a combobox and display it at startup

musoguy

Board Regular
Joined
May 20, 2008
Messages
173
I found this very clever piece of code that sets up a list in a combobox (in a userform) with all the Printers on the user's machine. There are a couple of questions I had that I hope someone will be able to answer as this is a little beyond my level of very limited expertise.

Firstly I would like the computer's Default Printer to appear in the Combobox when the userform starts but have no clue how to do this. My guess is it will have something to do with matching the name from the list with "Application.ActivePrinter" but that's all I got!

Secondly, when the user chooses their printer how do I then print from that choice?

The code is below:
Code:
Option Explicit
'Place the code below into the userform module
'Please make Userform with Combobox1 before execute this 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 CommandButton2_Click()

End Sub

Private Sub ComboBox1_Change()

End Sub

Private Sub CommandButton1_Click()

End Sub

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
'    Me.ComboBox1.ListIndex = 0 - gives me the 1st item in the list but that is not what I want
End Sub
Hope someone can help :)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
For the first part you can just set the value in the combobox.
Code:
Me.ComboBox1.Value = Application.ActivePrinter

Not sure what you mean about printing from that printer, unless you specify another printer that's the one which will be used.
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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