How to create and print barcodes with Visual Basic 6 and Bytescout BarCode SDK - ByteScout

How to create and print barcodes with Visual Basic 6 and Bytescout BarCode SDK

  • Home
  • /
  • Articles
  • /
  • How to create and print barcodes with Visual Basic 6 and Bytescout BarCode SDK

The following sample code for Visual Basic 6 demonstrates how to use Barcode SDK in Visual Basic 6 to generate and print barcodes from VB6 code

	VERSION 5.00

Begin VB.Form <span data-scayt_word="Form1" data-scaytid="2">Form1</span> 
   <span data-scayt_word="AutoRedraw" data-scaytid="6">AutoRedraw</span>      =   -1  'True
   Caption         =   "<span data-scayt_word="Form1" data-scaytid="3">Form1</span>"

   <span data-scayt_word="ClientHeight" data-scaytid="7">ClientHeight</span>    =   4845
   <span data-scayt_word="ClientLeft" data-scaytid="8">ClientLeft</span>      =   45
   <span data-scayt_word="ClientTop" data-scaytid="9">ClientTop</span>       =   435

   <span data-scayt_word="ClientWidth" data-scaytid="10">ClientWidth</span>     =   9795
   <span data-scayt_word="LinkTopic" data-scaytid="11">LinkTopic</span>       =   "<span data-scayt_word="Form1" data-scaytid="4">Form1</span>"
   <span data-scayt_word="ScaleHeight" data-scaytid="12">ScaleHeight</span>     =   4845

   <span data-scayt_word="ScaleWidth" data-scaytid="13">ScaleWidth</span>      =   9795
   <span data-scayt_word="StartUpPosition" data-scaytid="14">StartUpPosition</span> =   3  'Windows Default
   Begin VB.CommandButton <span data-scayt_word="Command1" data-scaytid="15">Command1</span> 
      Caption         =   "Print Barcode to the selected printer"

      Height          =   495
      Left            =   3840
      <span data-scayt_word="TabIndex" data-scaytid="16">TabIndex</span>        =   1

      Top             =   240
      Width           =   2895
   End
   Begin VB.ComboBox <span data-scayt_word="cboPrinterList" data-scaytid="18">cboPrinterList</span> 
      Height          =   315

      Left            =   120
      <span data-scayt_word="TabIndex" data-scaytid="17">TabIndex</span>        =   0
      Top             =   360

      Width           =   3135
   End
End
Attribute VB_Name = "<span data-scayt_word="Form1" data-scaytid="5">Form1</span>"

Attribute <span data-scayt_word="VB_GlobalNameSpace" data-scaytid="19">VB_GlobalNameSpace</span> = False
Attribute <span data-scayt_word="VB_Creatable" data-scaytid="20">VB_Creatable</span> = False
Attribute <span data-scayt_word="VB_PredeclaredId" data-scaytid="21">VB_PredeclaredId</span> = True

Attribute VB_Exposed = False
 ' IMPORTANT NOTE: you need to have .NET Framework 1.10 installed to use <span data-scayt_word="BarCode" data-scaytid="22">BarCode</span> SDK from Visual Basic
 ' to download and install .NET Framework 1.10 please use this link: http://www.microsoft.com/downloads/details.aspx?<span data-scayt_word="familyid" data-scaytid="23">familyid</span>=<span data-scayt_word="262D25E3-F589-4842-8157-034D1E7CF3A3" data-scaytid="24">262D25E3-F589-4842-8157-034D1E7CF3A3</span>

Private Sub <span data-scayt_word="Command1_Click" data-scaytid="25">Command1_Click</span>()

  ' set printer to the selected printer
  Call <span data-scayt_word="SelectPrinter" data-scaytid="26">SelectPrinter</span>(cboPrinterList.Text)
  ' call function to print barcode to Printer object
 Call <span data-scayt_word="PrintBarCode" data-scaytid="30">PrintBarCode</span>

End Sub

 ' this sample will print one single page with code 39 and Aztec <span data-scayt_word="2D" data-scaytid="32">2D</span> barcode on default printer
 
' populate list of printers on form load
Private Sub Form_Load()

Dim <span data-scayt_word="dev" data-scaytid="35">dev</span> As Printer, Index As Integer, <span data-scayt_word="CurrentPrinter" data-scaytid="37">CurrentPrinter</span> As Integer

<span data-scayt_word="CurrentPrinter" data-scaytid="38">CurrentPrinter</span> = -1
Index = 0
For Each <span data-scayt_word="dev" data-scaytid="36">dev</span> In Printers
cboPrinterList.AddItem dev.DeviceName

If Printer.DeviceName = dev.DeviceName Then
<span data-scayt_word="CurrentPrinter" data-scaytid="39">CurrentPrinter</span> = Index
End If

       Index = Index + 1
Next
If <span data-scayt_word="CurrentPrinter" data-scaytid="40">CurrentPrinter</span> <> -1 Then

 cboPrinterList.ListIndex = CurrentPrinter
End If

End Sub
 

'===========================
'Function to select printer
'===========================

Function SelectPrinter(ByVal printer_name As String) As Boolean

    Dim i As Integer

    SelectPrinter = False
    For i = 0 To Printers.Count - 1

        ' if the specified printer is found, select it and return True
        If Printers(i).DeviceName = printer_name Then

            Set Printer = Printers(i)
            SelectPrinter = True

            Exit For
        End If
    Next i
End Function


' print barcode to the Printer object

Private Sub PrintBarCode()
 
 ' IMPORTANT: call Printer.Print Space(1) to initialize the hDC of the Printer
 ' this is how Microsoft advises to do in their KB article (search for Printer.Print string): http://support.microsoft.com/kb/146022

 Printer.Print Space(1) ' initialize hDC of Printer object

 
 ' create barcode object
 Set bc = CreateObject("Bytescout.BarCode.Barcode")

 ' set symbology type
 bc.Symbology = 1 ' 1 = Code39

 ' set value to encode

 bc.Value = "012345"
  
 ' draw code 39 barcode on a page
 bc.DrawHDC Printer.hDC, 0, 0

 
 ' now drawing 2D Aztec barcode
 
 ' set symbology type
 bc.Symbology = 17 ' 17 = Aztec
 ' set value to encode

 bc.Value = "012345"

 ' draw Aztec 2D barcode on a page
 bc.DrawHDC Printer.hDC, 0, 300

 
 Printer.Print "This is a test barcode"
 
 ' finally send command to print the page
 Printer.EndDoc

 Set bc = Nothing

End Sub

Tutorials:

prev
next