set printer from combobox

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

set printer from combobox

Post by sal21 »

I loop all disponible printers:

Code: Select all

Sub CARICA_PRINTER()

    On Error GoTo Err_SomeName

    If Me.COMBO_PRINTER.ListCount = 0 Then
        For Each P In Printers
            Me.COMBO_PRINTER.AddItem UCase(P.DeviceName)
        Next
    End If

Exit_SomeName:
    Exit Sub

Err_SomeName:
    MsgBox Err.Number & Err.Description
    Resume Exit_SomeName

End Sub


how to set the printer in selected combobox?

i use this code but not work!

Code: Select all


Sub PrintToPrinter()

    On Error GoTo PrintError
    
    Set Printer = Me.COMBO_PRINTER <<<<< error here
    
    Printer.PaperSize = vbPRPSA4
    Printer.FontName = "Courier New"
    Printer.FontSize = "9"
    Printer.Orientation = 2

    If InStr(Me.SCROLL_OP.Text, "APERTURA ORDINE NUMERO:") > 0 Then
        Printer.Print vbNewLine &[b] "DATA CONTABILE " & Me.DATA_CONT.Caption & Space(5) & "AGENZIA " & Me.AGENZIA.Caption & Space(10) & "MATRICOLA " & Me.Label5.Caption & Space(5) & "STAMPATO IL " & Format(CDate(Now), "DD/MM/YYYY") & Space(5) & "NR. OPERAZ. " & CONTA1 & [/b]Chr(13) & Chr(13) & Me.SCROLL_OP.Text
    Else
        Printer.Print vbNewLine &[b] "DATA CONTABILE " & Me.DATA_CONT.Caption & Space(5) & "AGENZIA " & Me.AGENZIA.Caption & Space(10) & "MATRICOLA " & Me.Label5.Caption & Space(20) & "STAMPATO IL " & Format(CDate(Now), "DD/MM/YYYY")[/b] & Chr(13) & Chr(13) & Me.SCROLL_OP.Text
    End If

    Printer.EndDoc

    Exit Sub

PrintError:
    MsgBox "Error number: " & Err.Number & vbCrLf & "Description: " & Err.Description & "" & Chr(13) & "" & Chr(13) & "May be printer is still off or out of paper." & Chr(13) & "Please turn on your printer now or fill in " & Chr(13) & "the paper to printer. Then, try again.", vbCritical, "Printer Error"
    Exit Sub

End Sub
is possible to print in
Printer.FontName = "Courier New"
Printer.FontSize = "9"
and bold only the line signed with bold marked code?
Last edited by sal21 on 18 Jan 2012, 15:46, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: set printer from combobox

Post by HansV »

What happens if you use

Set Printer = Printers(Me.COMBO_PRINTER)

or

Set Printer = Printers(Me.COMBO_PRINTER.ListIndex)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: set printer from combobox

Post by sal21 »

HansV wrote:What happens if you use

Set Printer = Printers(Me.COMBO_PRINTER)

or

Set Printer = Printers(Me.COMBO_PRINTER.ListIndex)
i use the second option of your code and work perfect!
Tks. as usual. :thankyou: :clapping: