LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Gary Brown
 
Posts: n/a
Default

Marc,
I have a rather complicated macro that lists ALL your printers. Copy this
to a blank module (create it using the 'Insert Module' technique) and run the
'Printer_List' macro (part way down the page) by putting your cursor on the
line that says...

Sub Printer_List()

and Press the 'F5' key.
--
Gary Brown



'/ START COPYING HERE ---------------------------------------/

Const PRINTER_ENUM_CONNECTIONS = &H4
Const PRINTER_ENUM_LOCAL = &H2

Private 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

Private Declare Function PtrToStr _
Lib "kernel32" Alias "lstrcpyA" _
(ByVal RetVal As String, _
ByVal Ptr As Long) As Long

Private Declare Function StrLen _
Lib "kernel32" Alias "lstrlenA" _
(ByVal Ptr As Long) As Long
'

'/=============================================/

Public Function ListPrinters() As Variant

Dim bSuccess As Boolean
Dim iBufferRequired As Long
Dim iBufferSize As Long
Dim iBuffer() As Long
Dim iEntries As Long
Dim iIndex As Long
Dim strPrinterName As String
Dim iDummy As Long
Dim iDriverBuffer() As Long
Dim strPrinters() As String

iBufferSize = 3072

ReDim iBuffer((iBufferSize \ 4) - 1) As Long

'EnumPrinters will return a value False if the
' buffer is not big enough
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, vbNullString, _
1, iBuffer(0), iBufferSize, _
iBufferRequired, iEntries)

If Not bSuccess Then
If iBufferRequired iBufferSize Then
iBufferSize = iBufferRequired
Debug.Print _
"iBuffer too small. Trying again with "; _
iBufferSize & " bytes."
ReDim iBuffer(iBufferSize \ 4) As Long
End If
'Try again with new buffer
bSuccess = EnumPrinters(PRINTER_ENUM_CONNECTIONS Or _
PRINTER_ENUM_LOCAL, vbNullString, _
1, iBuffer(0), iBufferSize, iBufferRequired, iEntries)
End If

If Not bSuccess Then
'Enumprinters returned False
MsgBox "Error enumerating printers."
Exit Function
Else
'Enumprinters returned True,
' use found printers to fill the array
ReDim strPrinters(iEntries - 1)
For iIndex = 0 To iEntries - 1
'Get the printername
strPrinterName = Space$(StrLen(iBuffer(iIndex * 4 + 2)))
iDummy = PtrToStr(strPrinterName, iBuffer(iIndex * 4 + 2))
strPrinters(iIndex) = strPrinterName
Next iIndex
End If

ListPrinters = strPrinters

End Function
'/===============================================/


'You could call the function as follows:

'/============================================/
Sub Printer_List()

Dim x As Long, i As Long
Dim strPrinterList As String
Dim strAnswer As String, strAnswer1 As String
Dim strAnswer2 As String, strAnswer3 As String
Dim varPrinters As Variant

strAnswer1 = "Show in Message Box"
strAnswer2 = "Put on Worksheet at current location"
strAnswer3 = "Cancel"

strAnswer = _
Wksht_or_Msgbox(strAnswer1, strAnswer2, strAnswer3)

If strAnswer = strAnswer3 Then
Exit Sub
End If

varPrinters = ListPrinters
strPrinterList = ""
'Fist check whether the array is filled with anything,
' by calling another function, IsBounded.

Select Case strAnswer
Case strAnswer1
If IsBounded(varPrinters) Then
strPrinterList = "Available Printers: "
For x = LBound(varPrinters) To UBound(varPrinters)
strPrinterList = _
strPrinterList & vbCr & varPrinters(x)
Next x
strPrinterList = strPrinterList & vbCr & vbCr & _
"Active Printer: " & Application.ActivePrinter
Else
strPrinterList = _
strPrinterList & vbCr & "No printers found"
End If
MsgBox strPrinterList

Case strAnswer2
ActiveCell.value = "Available Printers: "
If IsBounded(varPrinters) Then
For x = LBound(varPrinters) To UBound(varPrinters)
i = i + 1
ActiveCell.Offset(i, 0).value = varPrinters(x)
Next x
ActiveCell.Offset(i + 2, 0).value = _
"Active Printer: " & Application.ActivePrinter
Else
ActiveCell.value = "No printers found"
End If

Case strAnswer3
Exit Sub
Case Else
Exit Sub

End Select

End Sub

'/==============================================/
Public Function IsBounded(vArray As Variant) As Boolean

'If the variant passed to this function is an array,
' the function will return True;
' otherwise it will return False
On Error Resume Next
IsBounded = IsNumeric(UBound(vArray))

End Function
'/==================================================/
Function Wksht_or_Msgbox(str1, str2, str3) As String
'Adds choices as defined in Ops array below
Dim aryChoices()
Dim iMaxChoices As Double
Dim strTitle As String
Dim varChoiceSelected As Variant

iMaxChoices = 3
strTitle = "Printer List Output..."

ReDim aryChoices(1 To iMaxChoices)

aryChoices(1) = str1
aryChoices(2) = str2
aryChoices(3) = str3

'Array of choices, default choice, title of form
varChoiceSelected = GetChoice(aryChoices, 1, strTitle)
' MsgBox aryChoices(varChoiceSelected)
Wksht_or_Msgbox = aryChoices(varChoiceSelected)
End Function

'/===END COPYING HERE============================/




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change Series order in a Combination Chart? vrk1 Charts and Charting in Excel 3 April 1st 05 07:21 AM
series graph -- one series being added to another series rich zielinski via OfficeKB.com Charts and Charting in Excel 3 March 30th 05 06:23 PM
Playing a macro from another workbook Jim Excel Discussion (Misc queries) 1 February 23rd 05 10:12 PM
Date macro Hiking Excel Discussion (Misc queries) 9 February 3rd 05 12:40 AM
How to program an excel macro to repeat a series of keystrokes? Beancounter Excel Discussion (Misc queries) 8 January 22nd 05 11:51 PM


All times are GMT +1. The time now is 07:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"