VBA/Excel/Access/Word/Windows API/Printer

Материал из VB Эксперт
Перейти к: навигация, поиск

Get default printer information

 
Private Declare Function GetProfileStringA Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Sub DefaultPrinterInfo()
    Dim strLPT As String * 255
    Dim Result As String
    Call GetProfileStringA ("Windows", "Device", "", strLPT, 254)
    
    Result = Application.Trim(strLPT)
    ResultLength = Len(Result)
    Comma1 = Application.Find(",", Result, 1)
    Comma2 = Application.Find(",", Result, Comma1 + 1)
"   Gets printer"s name
    Printer = Left(Result, Comma1 - 1)
"   Gets driver
    Driver = Mid(Result, Comma1 + 1, Comma2 - Comma1 - 1)
"   Gets last part of device line
    Port = Right(Result, ResultLength - Comma2)
    Debug.Print Printer
    Debug.Print Driver
    Debug.Print Port
End Sub



Loop through all printer

 
Sub TestPrinter()
    Dim prtAvailPrinters As Printer
    For Each prtAvailPrinters In Application.Printers
        With prtAvailPrinters
            Debug.Print "Printer name: " & .DeviceName & "Printer driver: " & .DriverName
        End With
    Next prtAvailPrinters
End Sub



Modify Printer Settings

 
Sub ModifyPrinterSettings()
    
    Dim prtPrinter As Printer
    
    Set prtPrinter = Application.Printers(0)
    
    prtPrinter.TopMargin = 500
    prtPrinter.BottomMargin = 250
    prtPrinter.LeftMargin = 500
    prtPrinter.RightMargin = 500
    prtPrinter.Copies = 5
    
End Sub



The Printer Object

 
Private Sub cmdPrinterObject_Click()
    Dim prt As Printer
    Set prt = Printers(0)
    Debug.Print prt.DeviceName 
    Debug.Print prt.Port
    Debug.Print prt.ColorMode
    Debug.Print prt.Copies
End Sub



The Printers Collection

 
Private Sub cmdPrintersCollection_Click()
    Dim prt As Printer
    For Each prt In Printers
        Debug.Print prt.DeviceName
        Debug.Print prt.Port
    Next prt
End Sub