Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. Option Explicit
  2. Public Function Code128(SourceString As String)
  3. 'Written by Philip Treacy, Feb 2014
  4. 'http://www.myonlinetraininghub.com/create-barcodes-with-excel-vba
  5. 'This code is not guaranteed to be error free. No warranty is implied or expressed. Use at your own risk and carry out your own testing
  6. 'This function is governed by the GNU Lesser General Public License (GNU LGPL) Ver 3
  7. 'Input Parameters : A string
  8. 'Return : 1. An encoded string which produces a bar code when dispayed using the CODE128.TTF font
  9. ' 2. An empty string if the input parameter contains invalid characters
  10. Dim Counter As Integer
  11. Dim CheckSum As Long
  12. Dim mini As Integer
  13. Dim dummy As Integer
  14. Dim UseTableB As Boolean
  15. Dim Code128_Barcode As String
  16. If Len(SourceString) > 0 Then
  17. 'Check for valid characters
  18. For Counter = 1 To Len(SourceString)
  19. Select Case Asc(Mid(SourceString, Counter, 1))
  20. Case 32 To 126, 203
  21. Case Else
  22. MsgBox "Invalid character in barcode string." & vbCrLf & vbCrLf & "Please only use standard ASCII characters", vbCritical
  23. Code128 = ""
  24. Exit Function
  25. End Select
  26. Next
  27. Code128_Barcode = ""
  28. UseTableB = True
  29. Counter = 1
  30. Do While Counter <= Len(SourceString)
  31. If UseTableB Then
  32. 'Check if we can switch to Table C
  33. mini = IIf(Counter = 1 Or Counter + 3 = Len(SourceString), 4, 6)
  34. GoSub testnum
  35. If mini% < 0 Then 'Use Table C
  36. If Counter = 1 Then
  37. Code128_Barcode = Chr(205)
  38. Else 'Switch to table C
  39. Code128_Barcode = Code128_Barcode & Chr(199)
  40. End If
  41. UseTableB = False
  42. Else
  43. If Counter = 1 Then Code128_Barcode = Chr(204) 'Starting with table B
  44. End If
  45. End If
  46. If Not UseTableB Then
  47. 'We are using Table C, try to process 2 digits
  48. mini% = 2
  49. GoSub testnum
  50. If mini% < 0 Then 'OK for 2 digits, process it
  51. dummy% = Val(Mid(SourceString, Counter, 2))
  52. dummy% = IIf(dummy% < 95, dummy% + 32, dummy% + 100)
  53. Code128_Barcode = Code128_Barcode & Chr(dummy%)
  54. Counter = Counter + 2
  55. Else 'We haven't got 2 digits, switch to Table B
  56. Code128_Barcode = Code128_Barcode & Chr(200)
  57. UseTableB = True
  58. End If
  59. End If
  60. If UseTableB Then
  61. 'Process 1 digit with table B
  62. Code128_Barcode = Code128_Barcode & Mid(SourceString, Counter, 1)
  63. Counter = Counter + 1
  64. End If
  65. Loop
  66. 'Calculation of the checksum
  67. For Counter = 1 To Len(Code128_Barcode)
  68. dummy% = Asc(Mid(Code128_Barcode, Counter, 1))
  69. dummy% = IIf(dummy% < 127, dummy% - 32, dummy% - 100)
  70. If Counter = 1 Then CheckSum& = dummy%
  71. CheckSum& = (CheckSum& + (Counter - 1) * dummy%) Mod 103
  72. Next
  73. 'Calculation of the checksum ASCII code
  74. CheckSum& = IIf(CheckSum& < 95, CheckSum& + 32, CheckSum& + 100)
  75. 'Add the checksum and the STOP
  76. Code128_Barcode = Code128_Barcode & Chr(CheckSum&) & Chr$(206)
  77. End If
  78. Code128 = Code128_Barcode
  79. Exit Function
  80. testnum:
  81. 'if the mini% characters from Counter are numeric, then mini%=0
  82. mini% = mini% - 1
  83. If Counter + mini% <= Len(SourceString) Then
  84. Do While mini% >= 0
  85. If Asc(Mid(SourceString, Counter + mini%, 1)) < 48 Or Asc(Mid(SourceString, Counter + mini%, 1)) > 57 Then Exit Do
  86. mini% = mini% - 1
  87. Loop
  88. End If
  89. Return
  90. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement