Generating Code 128 Barcode using Microsoft Word

839 Views Asked by At

I'm trying to generate a Code 128 Barcode using Micorosft Word 2013 or 2019 and VBA using the following function:

 Public Function GenerateCode128B(SourceStruing As String)
 
 Dim checkDigitValue As Integer
 Dim barcodeString As String
 Dim startSign As String
 Dim endSign As String
 Dim index As Integer
 Dim c As Integer
  
 checkDigitValue = 104
 startSign = Chr(204)
 endSign = Chr(206)
 index = 1
  
 barcodeString = startSign
  
 For c = 1 To Len(SourceString) Step 1
  Dim currentSign As String
  currentSign = Asc(Mid(SourceString, c, 1))
   
  If Asc(currentSign) < 32 Or Asc(currentSign) > 126 Then
   GenerateCode128B = Empty
  Else
   barcodeString = barcodeString & Mid(SourceString, c, 1)
   checkDigitValue = checkDigitValue + (Asc(currentSign) - 32) * index
   index = index + 1
  End If
 Next
 
 checkDigitValue = checkDigitValue Mod 103
 
 If checkDigitValue > 94 Then
  checkDigitValue = checkDigitValue + 100
 Else
  checkDigitValue = checkDigitValue + 32
 End If
 
 barcodeString = barcodeString & Chr(checkDigitValue) & endSign
 
 GenerateCode128B = barcodeString
End Function

Therefore, I'm using this font: https://www.dafont.com/de/code-128.font.

To display the barcode in the word document, I use these two similar lines:

Selection.Font.Name = "Code 128"
Selection.TypeTest GenerateCode128B("ABC12DEF456G")

My current problem: If I do not install the font under Windows 10, the barcode is correct. If I install the font, the barcode is not readable.

Example: Plain text: ABC12DEF456F

Barcode without using a specific font: ÌABC12DEF456FNÎ (Correct and readable)

Barcode using the code128.ttf font: ƎΒΧΓƎΑΓ12ΔΕΖ456ΔƎΒΧΓƎ (Incorrect, unreadable!)

What could be the reason why the barcode is displayed with symbols of the greek alphabet?

0

There are 0 best solutions below