selecting printer

C

Chris

Hi,

I'm using a Macro to select a printet. The code I use is:

Sheets("PRINT").Select
Application.ActivePrinter = "DYMO LabelWriter 310 on
Ne01:"

The Ne01: port changes once in a while causing the macro
to stop and generates an error.
Is there another way to select a printer?

Thanks.
 
J

Jim Rech

This may help but you'll have to test it. You pass this function a partial
printer name (and a wildcard *) and it returns the first printer name that
matches it.

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("DYMO*")
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
| Hi,
|
| I'm using a Macro to select a printet. The code I use is:
|
| Sheets("PRINT").Select
| Application.ActivePrinter = "DYMO LabelWriter 310 on
| Ne01:"
|
| The Ne01: port changes once in a while causing the macro
| to stop and generates an error.
| Is there another way to select a printer?
|
| Thanks.
 

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