print in bold a part of string....

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

print in bold a part of string....

Post by sal21 »

I use to print the content of textbox and a header line.
thsi is my code:

Code: Select all

Sub PrintToPrinter()

    On Error GoTo PrintError

    Set Printer = Printers(Me.COMBO_PRINTER.ListIndex)

    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 & "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 & Chr(13) & Chr(13) & Me.SCROLL_OP.Text
    Else
        Printer.Print vbNewLine & "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") & 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

in this case this part is a header of print job:

Code: Select all

 "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
is a header of printer job and i want to print in bold, is possible?

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

Re: print in bold a part of string....

Post by HansV »

I can't test this, but try the following. Change

Code: Select all

        Printer.Print vbNewLine & "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 & Chr(13) & Chr(13) & Me.SCROLL_OP.Text
to

Code: Select all

        ' Turn bold ON
        Printer.FontBold = True
        ' Print the header
        Printer.Print vbNewLine & "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
        ' Turn bold OFF
        Printer.FontBold = False
        ' Print the rest
        Printer.Print Chr(13) & Chr(13) & Me.SCROLL_OP.Text
Best wishes,
Hans

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

Re: print in bold a part of string....

Post by sal21 »

HansV wrote:I can't test this, but try the following. Change

Code: Select all

        Printer.Print vbNewLine & "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 & Chr(13) & Chr(13) & Me.SCROLL_OP.Text
to

Code: Select all

        ' Turn bold ON
        Printer.FontBold = True
        ' Print the header
        Printer.Print vbNewLine & "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
        ' Turn bold OFF
        Printer.FontBold = False
        ' Print the rest
        Printer.Print Chr(13) & Chr(13) & Me.SCROLL_OP.Text

YOU CANNOT TEST!
The code work perfect!
Tks. :thankyou:

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

Re: print in bold a part of string....

Post by sal21 »

HansV wrote:I can't test this, but try the following. Change

Code: Select all

        Printer.Print vbNewLine & "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 & Chr(13) & Chr(13) & Me.SCROLL_OP.Text
to

Code: Select all

        ' Turn bold ON
        Printer.FontBold = True
        ' Print the header
        Printer.Print vbNewLine & "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
        ' Turn bold OFF
        Printer.FontBold = False
        ' Print the rest
        Printer.Print Chr(13) & Chr(13) & Me.SCROLL_OP.Text
Based the code above (last code):

Code: Select all

Sub PrintToPrinter()

    On Error GoTo PrintError

    Set Printer = Printers(Me.COMBO_PRINTER.ListIndex)

    Dim NOME_STAMPANTE As String
    NOME_STAMPANTE = Me.COMBO_PRINTER.Text

    Printer.PaperSize = vbPRPSA4
    Printer.FontName = "Courier New"
    Printer.FontSize = "9"
    Printer.Orientation = 2
    Printer.FontBold = False

    If InStr(Me.SCROLL_OP.Text, "APERTURA ORDINE NUMERO:") > 0 Then

        Printer.FontBold = True

        Printer.Print vbNewLine & "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

        Printer.FontBold = False

        Printer.Print Chr(13) & Chr(13) & Me.SCROLL_OP.Text
        Printer.EndDoc
        MsgBox ("ORDINE STAMPATO SU: " & NOME_STAMPANTE)
    Else
        Printer.FontBold = True
        Printer.Print vbNewLine & "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")

        Printer.FontBold = False
        Printer.Print Chr(13) & Chr(13) & Me.SCROLL_OP.Text
        Printer.EndDoc
        MsgBox ("OPERAZIONE/I STAMPATE SU: " & NOME_STAMPANTE)
    End If

    Exit Sub

PrintError:
    MsgBox "Errore numero: " & Err.Number & vbCrLf & "Descrizione: " & Err.Description & "" & Chr(13) & "" & Chr(13) & "CONTROLLARE STATO STAMPANTE!", vbCritical, "ERRORI STAMPANTE"
    Exit Sub

End Sub
- how to know the number of pages?
- how to know when code print the next page?

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

Re: print in bold a part of string....

Post by HansV »

You'll have to keep track of the line number and page number yourself, see for example Printing in VB6 using the Printer Object. (You can also use Printer.Page to get the current page number)

To get the total number of pages, I think you'll have to loop through the data twice, the first time to count the number of pages, the second time to actually print the data.
Best wishes,
Hans