Fuente: https://strokescribe.com/en/code-128-excel-word-access-html.html
CODE 128
CODE 128 is a simple and effective format to store small amounts of alphanumeric and ASCII control characters. CODE 128 offers three specialized alphabets: CODE 128A for ASCII control codes, digits and uppercase letter encoding, CODE 128B for uppercase and lowercase letters and digits, CODE 128C for double density numeric data encoding.
On the right: the top picture shows how a numeric-only string can be encoded in CODE 128C. The middle picture shows an alphanumeric string of the same length encoded in alphabet B.
Of course, CODE 128 allows to mix alphabets - we can start from alphabet C to encode a numeric substring and then switch to alphabet A or B to encode the rest of data to produce a smallest possible barcode (see the bottom picture). This is called alphabet auto-switching - any high quality CODE 128 encoder will do this to achieve maximum barcode density.
Alphabet subtype | What can be encoded |
---|---|
CODE 128A |
|
CODE 128B |
|
CODE 128C |
|
About maximum barcode length and character count. They are not standardized and limited by particular scanner capabilities. For example, Metrologic MS5145 is capable to scan 38 mm wide barcodes at face and 170 mm wide barcodes from the range of 140 mm. In the first case, bars must not be thinner than 0.1 mm and in the second case bars must not be thinner than 0.66 mm. In most cases, it's best to follow the GS1 recommendations for GS1-128 barcodes:
- The maximum physical length of barcode is 165 mm (6.5in) including quiet zones.
- The maximum number of encoded data characters is 48.
- The minimum module width is 0.250 mm and the maximum module width is 1.016 mm.
How the CODE 128 works
The CODE 128 encoding system consists of 3 code pages that share the same zebra patterns.
For example, the capital letter A is encoded as 10100011000 where each "1" represents a black bar and each "0" is a white bar. All bars must have the same width. A typical laser barcode scanner resolution is 0.15mm, so you can draw a barcode where each (black or white) bar has the width of 0.15mm.
On the right: 11101011 is a pattern you will find at the tail of any CODE 128 symbol. The "111010" sequence is a part of a STOP character and the final "11" (two black bars) is the TERM pattern.
Each CODE 128 symbol begins with a START-A (or START-B or START-C) character, continues with data characters and ends with a check-digit, a STOP character and a TERM pattern. The checksum covers the START character and all data characters multiplied by their position numbers. The check-digit is a reminder from division of the checksum by 103.
A real CODE 128 example: let's encode the capital letters ABC in a CODE 128A symbol.
Character | START-A | A | B | C | Check-digit | STOP | TERM |
---|---|---|---|---|---|---|---|
Code | 103 | 33 | 34 | 35 | 0 | 106 | 107 |
Position | - | 1 | 2 | 3 | |||
Checksum | 103 | 33*1=33 | 34*2=68 | 35*3=105 | 309%103=0 | ||
103 + 33 + 68 + 105 = 309 | |||||||
Character pattern | 11010000100 | 10100011000 | 10001011000 | 10001000110 | 11011001100 | 11000111010 | 11 |
Barcode pattern | 11010000100101000110001000101100010001000110110110011001100011101011 | ||||||
Printable zebra |
Note: the % sign is a "remainder from division" operator. For example, 103%103=0, 104%103=1, 207%103=1.
Another CODE 128 example: let's encode the abc<TAB>1234 in the barcode. Small letters can be encoded in the B code page, ASCII <TAB> character exists in the A code page and pairs of digits can be encoded using the C code page. So we start the CODE 128 symbol with the START-B instruction, encode the "abc" string, continue with the Code-A (see Table #3) instruction and encode the <TAB> character, use the Code-C instruction to encode the "1234" with only two character codes 12 and 34.
Character | START-B | a | b | c | Code-a | TAB | Code-C | 12 | 34 | Check-digit | STOP | TERM |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Code | 104 | 65 | 66 | 67 | 101 | 73 | 99 | 12 | 34 | 58 | 106 | 107 |
Position | - | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |||
Checksum | 104 | 65*1 =65 | 66*2 =132 | 67*3 =201 | 101*4 =404 | 73*5 =365 | 99*6 =594 | 12*7 =84 | 34*8 =272 | 2221%103 = 58 | ||
104 + 65 + 132 + 201 + 404 + 365 + 594 + 84 + 272 = 2221 |
Now, look up the encoding tables (see below) for zebra patterns for codes 104, 65, 66, 67, 101, 73, 99, 12, 34, 58, 106, 107. Combine these patterns into one string. Draw a black bar for each "1" and white bar for each "0".
CODE 128 encoding tables
Table #1
Code | CODE 128A | CODE 128B | CODE 128C | Zebra bits |
---|---|---|---|---|
0 | SP | SP | 00 | 11011001100 |
1 | ! | ! | 01 | 11001101100 |
2 | " | " | 02 | 11001100110 |
3 | # | # | 03 | 10010011000 |
4 | $ | $ | 04 | 10010001100 |
5 | % | % | 05 | 10001001100 |
6 | & | & | 06 | 10011001000 |
7 | ' | ' | 07 | 10011000100 |
8 | ( | ( | 08 | 10001100100 |
9 | ) | ) | 09 | 11001001000 |
10 | * | * | 10 | 11001000100 |
11 | + | + | 11 | 11000100100 |
12 | , | , | 12 | 10110011100 |
13 | - | - | 13 | 10011011100 |
14 | . | . | 14 | 10011001110 |
15 | / | / | 15 | 10111001100 |
16 | 0 | 0 | 16 | 10011101100 |
17 | 1 | 1 | 17 | 10011100110 |
18 | 2 | 2 | 18 | 11001110010 |
19 | 3 | 3 | 19 | 11001011100 |
20 | 4 | 4 | 20 | 11001001110 |
21 | 5 | 5 | 21 | 11011100100 |
22 | 6 | 6 | 22 | 11001110100 |
23 | 7 | 7 | 23 | 11101101110 |
24 | 8 | 8 | 24 | 11101001100 |
25 | 9 | 9 | 25 | 11100101100 |
26 | : | : | 26 | 11100100110 |
27 | ; | ; | 27 | 11101100100 |
28 | < | < | 28 | 11100110100 |
29 | = | = | 29 | 11100110010 |
30 | > | > | 30 | 11011011000 |
31 | ? | ? | 31 | 11011000110 |
32 | @ | @ | 32 | 11000110110 |
33 | A | A | 33 | 10100011000 |
34 | B | B | 34 | 10001011000 |
35 | C | C | 35 | 10001000110 |
36 | D | D | 36 | 10110001000 |
37 | E | E | 37 | 10001101000 |
38 | F | F | 38 | 10001100010 |
39 | G | G | 39 | 11010001000 |
40 | H | H | 40 | 11000101000 |
41 | I | I | 41 | 11000100010 |
42 | J | J | 42 | 10110111000 |
43 | K | K | 43 | 10110001110 |
44 | L | L | 44 | 10001101110 |
45 | M | M | 45 | 10111011000 |
46 | N | N | 46 | 10111000110 |
47 | O | O | 47 | 10001110110 |
Table #2
Code | CODE 128A | CODE 128B | CODE 128C | Zebra bits |
---|---|---|---|---|
48 | P | P | 48 | 11101110110 |
49 | Q | Q | 49 | 11010001110 |
50 | R | R | 50 | 11000101110 |
51 | S | S | 51 | 11011101000 |
52 | T | T | 52 | 11011100010 |
53 | U | U | 53 | 11011101110 |
54 | V | V | 54 | 11101011000 |
55 | W | W | 55 | 11101000110 |
56 | X | X | 56 | 11100010110 |
57 | Y | Y | 57 | 11101101000 |
58 | Z | Z | 58 | 11101100010 |
59 | [ | [ | 59 | 11100011010 |
60 | \ | \ | 60 | 11101111010 |
61 | ] | ] | 61 | 11001000010 |
62 | ^ | ^ | 62 | 11110001010 |
63 | _ | _ | 63 | 10100110000 |
64 | <NUL> (0x00) | ` | 64 | 10100001100 |
65 | <SOH> (0x01) | a | 65 | 10010110000 |
66 | <STX> (0x02) | b | 66 | 10010000110 |
67 | <ETX> (0x03) | c | 67 | 10000101100 |
68 | <EOT> (0x04) | d | 68 | 10000100110 |
69 | <ENQ> (0x05) | e | 69 | 10110010000 |
70 | <ACK> (0x06) | f | 70 | 10110000100 |
71 | <BEL> (0x07) | g | 71 | 10011010000 |
72 | <BS> (0x08) | h | 72 | 10011000010 |
73 | <HT> (0x09) | i | 73 | 10000110100 |
74 | <LF> (0x0a) | j | 74 | 10000110010 |
75 | <VT> (0x0b) | k | 75 | 11000010010 |
76 | <FF> (0x0c) | l | 76 | 11001010000 |
77 | <CR> (0x0d) | m | 77 | 11110111010 |
78 | <SO> (0x0e) | n | 78 | 11000010100 |
79 | <SI> (0x0f) | o | 79 | 10001111010 |
80 | <DLE> (0x10) | p | 80 | 10100111100 |
81 | <DC1> (0x11) | q | 81 | 10010111100 |
82 | <DC2> (0x12) | r | 82 | 10010011110 |
83 | <DC3> (0x13) | s | 83 | 10111100100 |
84 | <DC4> (0x14) | t | 84 | 10011110100 |
85 | <NAK> (0x15) | u | 85 | 10011110010 |
86 | <SYN> (0x16) | v | 86 | 11110100100 |
87 | <ETB> (0x17) | w | 87 | 11110010100 |
88 | <CAN> (0x18) | x | 88 | 11110010010 |
89 | <EM> (0x19) | y | 89 | 11011011110 |
90 | <SUB> (0x1a) | z | 90 | 11011110110 |
91 | <ESC> (0x1b) | { | 91 | 11110110110 |
92 | <FS> (0x1c) | | | 92 | 10101111000 |
93 | <GS> (0x1d) | } | 93 | 10100011110 |
94 | <RS> (0x1e) | ~ | 94 | 10001011110 |
95 | <US> (0x1f) | <DEL> (0x7f) | 95 | 10111101000 |
Table #3
Code | CODE 128A | CODE 128B | CODE 128C | Zebra bits |
---|---|---|---|---|
96 | FNC3 | FNC3 | 96 | 10111100010 |
97 | FNC2 | FNC2 | 97 | 11110101000 |
98 | SHIFT | SHIFT | 98 | 11110100010 |
99 | Code-C | Code-C | 99 | 10111011110 |
100 | Code-B | FNC4 | Code-B | 10111101110 |
101 | FNC4 | Code-A | Code-A | 11101011110 |
102 | FNC1 | FNC1 | FNC1 | 11110101110 |
103 | START-A | START-A | START-A | 11010000100 |
104 | START-B | START-B | START-B | 11010010000 |
105 | START-C | START-C | START-C | 11010011100 |
106 | STOP | STOP | STOP | 11000111010 |
107 | TERM | TERM | TERM | 11 |
Attention! The barcode must have whitespaces at the left/right sides equal to the width of 10 bars. Do not place any text or graphics on these areas.
Do not try to make your barcode as small as possible. Even the barcode scanner's resolution can be 0.1-0.15 mm, a cheap paper or ink will produce ragged barcode bars. Normally, good quality 0.1 mm barcodes may be printed only at printing office.
How to create CODE 128 in Microsoft Word with VBA
A minimalistic example of placing CODE 128 barcodes into a Word document using VBA and StrokeScribe ActiveX. If you need to place a single barcode or if you want to manually set barcode properties, use these guides for Word 2007, Word 2010 and Word 2013.
The code below uses the InlineShapes collection and places a barcode object at the current text input position. Also, the code demonstrates how to do the following tweaks:
- A CODE 128 produced with default ActiveX settings.
- A barcode rotated 90 degrees counterclockwise.
- The TextBelow property is used to show a user-specified text below the barcode.
- The text justification is turned of by TextJustify=False.
- The ProcessTilde property allows to encode ASCII control characters like <TAB>.
- Another way to encode ASCII control characters is to use Chr(). This barcode encodes <CR><LF>.
Application.ScreenUpdating = False
Dim doc As Document
Set doc = Word.ActiveDocument
Dim shp As InlineShape 'This shape type behaves as an ordinary text character
Set shp = doc.InlineShapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1", _
Range:=Selection.Range) 'Selection.Range points to the current text input position
Dim ss As StrokeScribe 'You will need to add a reference to StrokeScribe ActiveX into VBA project
Set ss = shp.OLEFormat.Object
ss.Alphabet = CODE128
ss.Text = "1234ABCDE"
'ss.Rotation = 90 'Uncomment this to rotate barcode 90 degrees counterclockwise (2)
'ss.TextBelow = "TEXT" 'Uncomment this to display a user-specified text below the barcode (3)
'ss.TextJustify = False 'Uncomment this to disable text justification (4)
ss.ProcessTilde = True 'This allows processing of tilde sequences (5)
ss.Text = "ABC~x09DEF" ' ~x09 encodes ASCII TAB character
ss.Text = "ABC" & Chr(13) & Chr(10) & "DEF" 'Another way to encode ASCII control codes (6)
shp.LockAspectRatio = msoFalse 'This allows to independently change width and height of the shape
shp.Width = Word.CentimetersToPoints(1)
shp.Height = Word.CentimetersToPoints(3)
Application.ScreenUpdating = True
Paste the code into a VBA procedure and add a reference to StrokeScribe ActiveX into your VBA project.
For bulk Code-128 printing in Microsoft Word, go here.
And use the following code to automatically delete all previously created barcode shapes:
Application.ScreenUpdating = False
For Each sh In ActiveDocument.InlineShapes
If sh.OLEFormat.ClassType = "STROKESCRIBE.StrokeScribeCtrl.1" Then
sh.Select
sh.Delete
End If
Next sh
Application.ScreenUpdating = True
To manually adjust the barcode properties, double-click the barcode picture.
To make a floating shape, right-click the barcode and select Format Control from the context menu. Switch to the Layout tab and choose a desired wrapping style. To do the same with VBA, execute:
Dim sh As Shape
shp.Select
Set sh = shp.ConvertToShape
sh.WrapFormat.Type = wdWrapNone
To change position of the floating shape, execute:
sh.Left = Application.CentimetersToPoints(5)
sh.Top = Application.CentimetersToPoints(10)
To delete barcodes converted to Shapes (not InlineShapes!), you need the different approach:
Set doc = Word.ActiveDocument
Dim sh As Shape
For i = doc.Shapes.Count To 1 Step -1
Set sh = doc.Shapes(i)
If sh.Type = msoEmbeddedOLEObject Then
If sh.OLEFormat.ClassType = "STROKESCRIBE.StrokeScribeCtrl.1" Then
sh.Delete
End If
End If
Next i
CODE 128 in Microsoft Excel
This will show how to use StrokeScribe ActiveX to create a single CODE 128 barcode from a cell data in Excel. For bulk CODE 128 printing, see next example. Additional examples of automatic barcode generation are available here.
If you need to manually place a single CODE 128 on Excel worksheet, see instructions how to do it in Excel 2007, Excel 2010 and Excel 2013. Also, you can install our barcode add-in to simplify this process.
If you aren't familiar with VBA development for Excel or if you want an example with statically placed ActiveX, see these step-by-step guides for Excel 2007, Excel 2010 and Excel 2013.
The VBA code shown below encodes content of the cell A1 in a vertically oriented CODE 128 barcode which is placed in the cell range B1:B8. To create a horizontally oriented barcode, set barcode.Rotation=0 and use a range like B1:B3
To run this code, add a reference to StrokeScribe ActiveX into your VBA project.
Application.ScreenUpdating = False 'This prevents screen flickering. Useful when creating/moving/deleting multiple ActiveX objects
Dim wsh As Worksheet
Set wsh = Excel.ActiveSheet 'A CODE 128 object will be placed on the currently active worksheet
'Use wsh=Excel.Worksheets(1) or wsh=Excel.Worksheets("Sheet1") to select a worksheet by index or by name
data = wsh.Cells(1, 1) 'or =wsh.Range("A1").Text
Dim r As Range
Set r = wsh.Range("B1:B8") 'A range of cells to fit the barcode
Dim shp As Shape
Set shp = wsh.Shapes.AddOLEObject("STROKESCRIBE.StrokeScribeCtrl.1")
shp.LockAspectRatio = msoFalse
shp.Width = r.Width 'The barcode will have the same position and size with the cell range B1:B8
shp.Height = r.Height
shp.Left = r.Left
shp.Top = r.Top
Dim barcode As StrokeScribe
Set barcode = shp.OLEFormat.Object.Object
barcode.Alphabet = CODE128
barcode.Text = data 'A text string from the cell A1
barcode.Rotation = 90 'The barcode is turned 90 degrees counterclockwise
'barcode.TextBelow = "text label" 'Uncomment this if you want a user-specified label below the barcode
'barcode.ProcessTilde = True 'Uncomment this if you need to encode ASCII control codes as tilde sequences
'barcode.Text = "ABC~x09DEF" 'A way to encode ASCII control codes in a plain text string (~x09 is ASCII TAB)
'barcode.Text = "ABC" & Chr(13) & Chr(1) & "DEF" 'Another way to use ASCII codes is to use the Chr() function
'This encodes <CR><LF>
Application.ScreenUpdating = True
If barcode.Error Then
MsgBox barcode.ErrorDescription
End If
To delete (or to modify) previously created CODE 128 barcodes, use the following VBA code:
Dim sh As Shape
For Each sh In Excel.ActiveSheet.Shapes
If sh.Type = msoOLEControlObject Then
If sh.OLEFormat.progID = "STROKESCRIBE.StrokeScribeCtrl.1" Then
Dim ss As StrokeScribe
Set ss = sh.OLEFormat.Object.Object
If ss.Alphabet = CODE128 Then
sh.Delete
End If
End If
End If
Next
If you want to place barcode at arbitrary position on Excel worksheet, modify the code as follows:
shp.Left = Application.InchesToPoints(1)
shp.Top = Application.InchesToPoints(1)
shp.Width = Application.CentimetersToPoints(4)
shp.Height = Application.CentimetersToPoints(2)
If you need to start printing from the same VBA script which modifies barcode properties, you need to know: Excel does not process the message queue while the VBA script is running, and, therefore, it does not refresh ActiveX controls. To force Excel to redraw a barcode, change it's size:
barcode.Text = data
tmp = barcode.Width
barcode.Width = 10
barcode.Width = tmp
wsh.PrintOut
For bulk CODE 128 printing in Microsoft Excel, go here.
Also, you may use formula to insert barcode into a cell. The example uses the same advanced technique as it described in the bulk barcode printing example.
How to use Data Binding in Excel
To synchronize a barcode object with a cell's content, you can use the LinkedCell property. For a manually inserted ActiveX object (see example for Excel 2013), set the LinkedCell property as shown on the picture.
If a barcode control is inserted using VBA code (see previous example), use the following code to bind it to a cell:
barcode.LinkedCell = "A1"
CODE 128 in Microsoft Access
This example shows how to create CODE 128 barcodes in Access 2007 and Access 2010 reports. For Access XP/2003, go to this article.
1. Open the report where you want to create CODE 128 barcodes and perform the following steps:
2. Switch to the Design tab. Click on the View button and select the Design View.
3.1. If you are working with Access 2007, click on the ActiveX Controls button as shown on the picture.
3.2. If you are working with Access 2010, do the following two steps:
3.2.1. Click on the More Controls button.
3.2.2. From the drop-down list, select ActiveX Controls.
4. From the list of available objects, select StrokeScribe Control.
5. Right-click on the barcode object and select Properties from the context menu to open the property sheet.
6. Switch to the Data tab on the Property Sheet window. Use the Control Source field to bind the barcode object to the desired database field.
7. Switch to the Other tab on the Property Sheet. Select CODE128 in the Alphabet field.
CODE 128 in HTML
An example how to show a CODE 128 barcode on HTML page. Because this example uses an ActiveX control to draw barcodes, it will work only with Internet Explorer. To run this example, copy the code into .hta file, this will disable numerous security warnings displayed by Internet Explorer. For .html files, you will need to modify some security settings in IE (see examples for IE 6 and IE 7 and 8).
<html>
<head>
<script>
function SetBarcode()
{
var barcode = document.getElementById("barcode");
barcode.Text = "1234567";
}
</script>
</head>
<body>
<button onclick="javascript:SetBarcode()">Start</button>
<object classid="CLSID:7E42B8C5-73BE-4806-8904-FF4080A6960C" id="barcode"
style="position: absolute; left: 20mm; top: 10mm; width: 15mm; height: 35mm">
<param name="Alphabet" value="5"/>
<param name="Text" value="1234ABCDEF"/>
<param name="Rotation" value="90"/>
</object>
</body>
</html>
No hay comentarios:
Publicar un comentario