Active Printer Application

L

Larry

I have a workbook with a VB code to choose a non-network printer and print a
document. The application code currently being used is:

Application.ActivePrinter = "hp photosmart p1110 series on LPT1:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"hp photosmart p1100 series on LPT1:", Collate:=True

This code works very well for this particular printer connected by parallel
cable to this particular port. However, I need an application syntax which
will work universally for any hp photosmart printer, disregarding any
particular model or whether it is a parallel or USB connection. Any
suggestions? Thanks for your help.
 
J

Jim Rech

You might try this code which attempts to return the name of an installed
printer that matches a partial name.


Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2
Declare Function EnumPrinters Lib "winspool.drv" Alias _
"EnumPrintersA" (ByVal flags As Long, _
ByVal xName 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

Sub TestFunc()
MsgBox GetPrnName("hp photosmart*")
End Sub

Function GetPrnName(Template As String) As String
Dim cbRequired As Long, cbBuffer As Long
Dim Buffer() As Long, nEntries As Long
Dim I As Long, PDesc As String, Try2 As Boolean
cbBuffer = 3000
TryAgain:
ReDim Buffer((cbBuffer \ 4) - 1)
If EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, "", 1, Buffer(0), cbBuffer, _
cbRequired, nEntries) Then
For I = 0 To nEntries - 1
PDesc = Space$(StrLen(Buffer(I * 4 + 2)))
PtrToStr PDesc, Buffer(I * 4 + 2)
If LCase(PDesc) Like LCase(Template) Then
GetPrnName = PDesc
Exit For
End If
Next
Else
If Not Try2 Then
Try2 = True
cbBuffer = cbRequired
GoTo TryAgain
End If
End If
End Function


--
Jim Rech
Excel MVP
|I have a workbook with a VB code to choose a non-network printer and print
a
| document. The application code currently being used is:
|
| Application.ActivePrinter = "hp photosmart p1110 series on LPT1:"
| ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
| "hp photosmart p1100 series on LPT1:", Collate:=True
|
| This code works very well for this particular printer connected by
parallel
| cable to this particular port. However, I need an application syntax
which
| will work universally for any hp photosmart printer, disregarding any
| particular model or whether it is a parallel or USB connection. Any
| suggestions? Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top