Application.ActivePrinter = "Printer Name in Quotes"
MsgBox Application.ActivePrinter
Debug.Print Application.ActivePrinter
Option Explicit
'Written by: Astrid Zeelenberg, http://word.mvps.org/
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
'Written by: Astrid Zeelenberg, http://word.mvps.org/
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
Sub Test()
'Written by: Astrid Zeelenberg, http://word.mvps.org/
'You could call the function as follows:
Dim StrPrinters As Variant, x As Long
StrPrinters = ListPrinters
'Fist check whether the array is filled with anything, by calling another function, IsBounded.
If IsBounded(StrPrinters) Then
For x = LBound(StrPrinters) To UBound(StrPrinters)
Debug.Print StrPrinters(x)
Next x
Else
Debug.Print "No printers found"
End If
End Sub
Public Function IsBounded(vArray As Variant) As Boolean
'Written by: Astrid Zeelenberg, http://word.mvps.org/
'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
Private Sub CommandButton1_Click()
Dim StrPrinters As Variant, x As Long
StrPrinters = ListPrinters
ThisWorkbook.ActiveSheet.Columns(1).Clear
'Fist check whether the array is filled with anything, by calling another function, IsBounded.
If IsBounded(StrPrinters) Then
For x = LBound(StrPrinters) To UBound(StrPrinters)
ThisWorkbook.ActiveSheet.Cells(x + 1, 1).Value = StrPrinters(x)
'Debug.Print StrPrinters(x)
Next x
Else
Debug.Print "No printers found"
End If
ThisWorkbook.ActiveSheet.Columns.AutoFit
End Sub
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim oCell As Range
Dim NoBlanks As Range
Set NoBlanks = ActiveSheet.Range("B7, D7, F7, H7, J7, L7, B9, D9, F9, H9, J9, L9, B11, D11, F11, H11, J11, L11, B13, D13, F13, H13, J13, L13, B15, D15, F15, H15, J15, L15, B17, D17, F17, H17, J17, L17") 'List all cells
For Each oCell In NoBlanks
If oCell.Value = "" Then
Cancel = True
MsgBox "Not all required cells have been filled", vbCritical + vbOKOnly, "Error"
Exit Sub
End If
Next
[B]Application.ActivePrinter = "\\sleepdc001.norwoodaustin.com\SLEE_RCV_CPY"[/B]
End Sub
Private Sub Workbook_Open()
Application.ActivePrinter = "\\sleepdc001.norwoodaustin.com\SLEE_RCV_CPY"
End Sub