How to Print from Visual Basic 6 - Print Both From Controls and Barcodes

This article is contributed by Dragan Knezevic from Community Forum. If you want to have further discussion with Dragan, visit this link.

Question

I am trying to print the barcode from VB and I am having some problems. If I print the form by calling PrintForm method, then I am not able to scan the barcode because of print quality. If I use the Printer object and manually code everything it gets more complicated because I want to print checkboxes and I also have to worry about word wrapping.

Answer

Below is the code that made it work. You are free to use it in your applications. If you have any questions or comments about the code you can reply to this post and I will try to answer them. Simply call PrintBarcode from your printing routine and pass a string that you want to generate a barcode for. In order to generate a barcode you would need to purchase Morovia Barcode Fonts and copy and paste Code39, SpecialChar functions from the source code into your VB project. Hope this helps.

Public Sub PrintBarcode(ByVal MSR As String)
' =========================================================================================
' Programmer:        Dragan Knezevic
' Date:              July 19 2004
' Description:       Prints the contents of labels, textboxes (with word wrapping), 
'                    checkboxes(caption only), DTPicker and generates a barcode for the
'                    passed parameter.
'
' Parameters Passed: MSR: A string for which a barcode needs to be generated.
' Value Returned:    None
' Called by:         PrintFrm or your own sub
' Calls:             FormatStringAndPrint
'
' Last Revised:      n/a
' Changes made:      n/a
' =========================================================================================
    Dim ctl As Control
    
    Printer.PrintQuality = vbPRPQHigh
    Printer.Orientation = vbPRORLandscape 'or vbPRORPortrait
    Printer.FontName = "MS Sans Serif"  'or any other font used for controls
    Printer.FontSize = 8
    Printer.ScaleMode = 1   'twips
    
    For Each ctl In frmPrintBarCode.Controls 'or your form name
        If TypeOf ctl Is SSResizer Then GoTo Res'or any other controls that are invisible at run-time (Timer)
      'should not be printed
        
        Printer.CurrentX = ctl.Left
        Printer.CurrentY = ctl.Top
 'add other controls here if you want to print their caption
        
        If TypeOf ctl Is Label Or TypeOf ctl Is CommandButton Or TypeOf ctl Is CheckBox Or TypeOf ctl Is Frame Then
            Printer.Print ctl.Caption
        ElseIf TypeOf ctl Is TextBox Then 'add other controls here if you want to print their text
            'this wraps the text inside a textbox so it fits nicely
     'carriage return/line feed characters are removed prior to formatting
     FormatStringAndPrint Replace(ctl.Text, vbCrLf, " "), ctl.Left, ctl.Width
     
     'this draws a line around the edges of the textbox
            Printer.Line (ctl.Left - 50, ctl.Top - 50)-(ctl.Left + ctl.Width + 50, ctl.Top + ctl.Height + 50), , B
        ElseIf TypeOf ctl Is DTPicker Then 'date picker control
            Printer.Print ctl.Value
            Printer.Line (ctl.Left - 25, ctl.Top - 25)-(ctl.Left + ctl.Width + 25, ctl.Top + ctl.Height + 25), , B
        End If
Res:
    Next
    
    'after we printed the contents of individual controls we are ready to generate the barcode
    Printer.FontName = "MRV Code39M"
    Printer.FontSize = 12
    
    Printer.CurrentX = 12700 'X position of the barcode in twips
    Printer.CurrentY = 10930 'Y position of the barcode in twips
    Printer.Print Code39(MSR) 'Code39 function is available for users that bought Morovia Barcode Fonts
    Printer.EndDoc
    
End Sub
Public Sub FormatStringAndPrint(sInput As String, TempX As Long, tempWidth As Long)
' =========================================================================================
' Programmer:        Dragan Knezevic
' Date:              July 20 2004
' Description:       Formats each texbox before printing a barcode so that the text
'                    wraps inside the control
'
' Parameters Passed: sInput: text inside of the control free of carriage returns.
'                    TempX : current x position of the control
'                    tempWidth: width of the control.
'
' Value Returned:    None
' Called by:         PrintBarcode
' Calls:             Nothing
'
' Last Revised:      n/a
' Changes made:      n/a
' =========================================================================================
    Dim sTemp As String
    Dim bSpace As Boolean
    
    Do
        If Printer.TextWidth(sInput$) <= tempWidth Then
                sTemp$ = Trim(sInput$): Exit Do
        End If
     
        Do
            sTemp$ = sTemp$ & Left(sInput$, 1)
            sInput$ = Right(sInput$, Len(sInput$) - 1)
            Debug.Print sTemp
            Debug.Print Printer.TextWidth(sTemp)
        Loop While Printer.TextWidth(sTemp$) < tempWidth - 100
        
        Do
            If Right(sTemp$, 1) = Chr(32) Then bSpace = True
            If Not bSpace Then
                sInput$ = Right(sTemp$, 1) & sInput$
            End If
            sTemp$ = Left(sTemp$, Len(sTemp$) - 1)
        Loop While Not bSpace
        
        bSpace = False
    
        Printer.Print sTemp$: sTemp$ = ""
        Printer.CurrentX = TempX
    Loop
    
    Printer.Print sTemp$
End Sub