0

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?

  • Probably some ASCII vs. WideChar or Unicode issue. Perhaps you need to run the output through [StrConv](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/strconv-function) with `vbUnicode` if you want to use that font. – John Coleman Sep 27 '21 at 09:37
  • 1
    If the barcode is correct and readable ***without*** using the font, then why use the font? By using the font, you make your code dependent on its being installed. – Timothy Rylatt Sep 27 '21 at 09:59
  • @Timothy Rylatt: The idea won't work, because "correct and readable" should refer to the plaintext. – TheSettler85 Sep 27 '21 at 10:44
  • @John Coleman: Thanks for the idea. It seems to be a conversion by Microsoft/ VBA refering to the "Selection.Font.Name = "Code 128"" statement in background. With StrConv(GenerateCode128B("ABC12DEF456G"), vbWide) I can reproduce the incorrect palin text ƎΒΧΓƎΑΓ12ΔΕΖ456ΔƎΒΧΓƎ. So I need to find a conversion from ƎΒΧΓƎΑΓ12ΔΕΖ456ΔƎΒΧΓƎ to ÌABC12DEF456FNÎ after changing the font. – TheSettler85 Sep 27 '21 at 10:49
  • 1
    How are you generating the plain text that includes the "symbols of the greek alphabet"? When I run your code then afterwards apply a standard font to the barcode the underlying text contains only the characters that you expect. – Timothy Rylatt Sep 27 '21 at 14:01
  • Also, have you considered using the [`DisplayBarcode`](https://support.microsoft.com/en-us/office/field-codes-displaybarcode-6d81eade-762d-4b44-ae81-f9d3d9e07be3#:~:text=Examples%20%20%20Code%20type%20%20%20DisplayBarcode,%E2%80%9C490123456789%E2%80%9D%20CODE128%20t%20%202%20more%20rows%20) field? That will display the barcode without using a special font. – Timothy Rylatt Sep 27 '21 at 14:15
  • @TimothyRylatt: Thanks for helping and the thought-provoking impulses. For my use case, using an inserted DisplayBarcode formula without having a special font works preety fine. It solves my problem. By the way: the other way trying to solve the problem with StrConv and vbUnicode, doesn't work. But it doesn't matter any more cause I have a solution. – TheSettler85 Sep 28 '21 at 10:20

0 Answers0