Barcode Generation in Microsoft Word with StrokeScribe Class

This example uses COM-class version of the StrokeScribe barcode generator to produce barcodes in Word.

Barcode generation in Word

How to Proceed

How it Works

The barcode generator object is invisible (non-ActiveX version) and produces barcode pictures in vector format in temporary files.

Once a barcode is created, the VBA code loads it into a picture shape. After that, the picture file may be deleted - the barcode is stored in the Word document.

The Code Example

Sub CreateBarcode() Dim doc As Document Set doc = Word.ActiveDocument On Error Resume Next ' deleting the previously created barcode Shapes("barcode").Delete On Error GoTo 0 Dim ss As StrokeScribeClass Set ss = CreateObject("STROKESCRIBE.StrokeScribeClass.1") ss.Alphabet = QRCODE ' or DATAMATRIX or CODE128 pict_file = Environ("TEMP") & "\bar.emf" Application.ScreenUpdating = False ss.Text = "ABCD" ' any text to encode in the barcode rc = ss.SavePicture(pict_file, EMF, 4, 4) If rc Then MsgBox ss.ErrorDescription Exit Sub End If Set sh = doc.Shapes.AddPicture(pict_file, False, True) sh.LockAspectRatio = msoFalse sh.Top = InchesToPoints(0.5) ' the barcode position - 0.5in x 0.5in from the top-left corner sh.Left = InchesToPoints(0.5) sh.Width = InchesToPoints(1) ' the barcode dimensions - 1x1in sh.Height = InchesToPoints(1) sh.Name = "barcode" Kill pict_file ' deleting the temporary picture file Application.ScreenUpdating = True End Sub

Related Tutorials