Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printer Names in Combo Box 1

Status
Not open for further replies.

SUnderwood

Programmer
Nov 21, 2002
107
GB
Hi All,

I'm having a great time trying to figure THIS one out!

Using VBA I want to put a list of all available printers into a combo box for my users to select from. I will then set the current/active printer to that selected and print to it.

Sounds easy? Yes, and so it should be, but NO! It’s a GHASTLY HORROR!!!! HELP!!!!!

Sean [greedo]



 
Here is the code I used, maybe you can scavenge what you need from it. I use Enumerate printers to populate a dropdown box named cbPrinters. Then I use ActivatPrtr to activate the printer the user has selected:

Option Explicit

Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2

Type PRINTER_INFO_1
flags As Long
pDescription As String
PName As String
pComment As String
End Type

Declare Function EnumPrinters Lib "winspool.drv" Alias _
"EnumPrintersA" (ByVal flags As Long, ByVal name As String, _
ByVal Level As Long, pPrinterEnum As Long, ByVal cdBuf As Long, _
pcbNeeded As Long, pcReturned As Long) As Long
Declare Function PtrToStr Lib "kernel32" Alias "lstrcpyA" _
(ByVal RetVal As String, ByVal Ptr As Long) As Long
Declare Function StrLen Lib "kernel32" Alias "lstrlenA" _
(ByVal Ptr As Long) As Long

Function EnumeratePrinters() As String()

Dim ListOfPrinters() As String
Dim Success As Boolean, cbRequired As Long, cbBuffer As Long
Dim Buffer() As Long, nEntries As Long
Dim I As Long, PFlags As Long, PDesc As String, PName As String
Dim pComment As String, Temp As Long

cbBuffer = 3072
ReDim Buffer((cbBuffer \ 4) - 1) As Long
Success = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or PRINTER_ENUM_LOCAL, _
vbNullString, 1, Buffer(0), cbBuffer, cbRequired, nEntries)

If Success Then
If cbRequired > cbBuffer Then
cbBuffer = cbRequired
Debug.Print "Buffer too small. Trying again with " & cbBuffer & " bytes."
ReDim Buffer(cbBuffer \ 4) As Long
Success = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or PRINTER_ENUM_LOCAL, _
vbNullString, 1, Buffer(0), cbBuffer, cbRequired, nEntries)
If Not Success Then
Debug.Print "Error enumerating printers."
Exit Function
End If
End If
Debug.Print "There are " & nEntries & " local and connected printers."

ReDim ListOfPrinters(nEntries - 1)

For I = 0 To nEntries - 1
PFlags = Buffer(4 * I)
PDesc = Space$(StrLen(Buffer(I * 4 + 1)))
Temp = PtrToStr(PDesc, Buffer(I * 4 + 1))
PName = Space$(StrLen(Buffer(I * 4 + 2)))
Temp = PtrToStr(PName, Buffer(I * 4 + 2))
pComment = Space$(StrLen(Buffer(I * 4 + 2)))
Temp = PtrToStr(pComment, Buffer(I * 4 + 2))
Debug.Print PFlags, PDesc, PName, pComment
ListOfPrinters(I) = PName
Next I
Else
Debug.Print "Error enumerating printers."
End If


EnumeratePrinters = ListOfPrinters

End Function


Sub ActivatePrtr()


On Error GoTo Network
Application.ActivePrinter = UserForm1.cbPrinters.Value & " on " & _
GetPrinterDetails(UserForm1.cbPrinters.Value).PortName
Exit Function

Network:
Dim blnWrongNe As Boolean
Dim I As Integer

On Error GoTo Err_Handler
Application.ActivePrinter = UserForm1.cbPrinters.Value & " on " & "Ne00:"
If blnWrongNe Then
blnWrongNe = False
For I = 1 To 9
Application.ActivePrinter = UserForm1.cbPrinters.Value & " on " & "Ne0" & I & ":"
If Not blnWrongNe Then Exit Function
blnWrongNe = False
Next I
End If

Err_Handler:
blnWrongNe = True
Resume Next

End Sub


 
Shane,

Sorry for the delayed reply, I've been on the road.

Have you been happy with the effectiveness of looping thru NE0 1 thru 9 til you get a hit?

I have been all over the web and the only other solution involes the GetPrinter API, testing the Share value and then using the UNC code combined with a scripting object.

THIS sounds much easier!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top