Using DLL version of the barcode generator in Excel

This example shows how to use plain DLL version of the StrokeScribe barcode generator in Excel.

Using DLL-based barcode generator to produce vector barcode picture

How To Proceed

The Code Example

The VBA code creates a barcode picture in vector format in a temporary file and then loads it into a Shape object. This method does not require ActiveX or font installation and produces small-sized, scalable barcodes.

#If VBA7 Then ' All Excel versions starting from Excel 2010 - 64-bit VBA overview #If Win64 Then ' The Declare statements in 64-bit apps must include the PtrSafe keyword. Private Declare PtrSafe Function Initialize Lib "StrokeScribeDL64.dll" () As Long Private Declare PtrSafe Function GetVer Lib "StrokeScribeDL64.dll" _ (ByRef major As Long, ByRef major2 As Long, ByRef minor As Long) As Long Private Declare PtrSafe Function SetLongPropU Lib "StrokeScribeDL64.dll" _ (ByVal name As LongPtr, ByVal val As Long) As Long Private Declare PtrSafe Function SetDoublePropU Lib "StrokeScribeDL64.dll" _ (ByVal name As LongPtr, ByVal val As Double) As Long Private Declare PtrSafe Function SetTextPropU Lib "StrokeScribeDL64.dll" _ (ByVal name As LongPtr, ByVal val As LongPtr) As Long Private Declare PtrSafe Function SetVariantPropU Lib "StrokeScribeDL64.dll" _ (ByVal name As LongPtr, ByRef val As Variant) As Long Private Declare PtrSafe Function GetLongPropU Lib "StrokeScribeDL64.dll" _ (ByVal name As LongPtr, ByRef val As Long) As Long Private Declare PtrSafe Function SavePictureU Lib "StrokeScribeDL64.dll" _ (ByVal filename As LongPtr, ByVal format As Long, ByVal w As Long, ByVal h As Long, _ ByVal dpi As Long) As Long Private Declare PtrSafe Function GetZebraBitsU Lib "StrokeScribeDL64.dll" _ (ByVal zebra As LongPtr, ByRef size As Long) As Long Private Declare PtrSafe Function GetFontOutU Lib "StrokeScribeDL64.dll" _ (ByVal out As LongPtr, ByRef size As Long) As Long Private Declare PtrSafe Function GetPictureArray Lib "StrokeScribeDL64.dll" _ (ByRef arr As Variant, ByVal format As Long, ByVal w As Long, ByVal h As Long, _ ByVal dpi As Long) As Long #Else ' Running on 32-bit version of Excel 2010+. Private Declare PtrSafe Function Initialize Lib "StrokeScribeDL.dll" () As Long Private Declare PtrSafe Function GetVer Lib "StrokeScribeDL.dll" _ (ByRef major As Long, ByRef major2 As Long, ByRef minor As Long) As Long Private Declare PtrSafe Function SetLongPropU Lib "StrokeScribeDL.dll" _ (ByVal name As LongPtr, ByVal val As Long) As Long Private Declare PtrSafe Function SetDoublePropU Lib "StrokeScribeDL.dll" _ (ByVal name As LongPtr, ByVal val As Double) As Long Private Declare PtrSafe Function SetTextPropU Lib "StrokeScribeDL.dll" _ (ByVal name As LongPtr, ByVal val As LongPtr) As Long Private Declare PtrSafe Function SetVariantPropU Lib "StrokeScribeDL.dll" _ (ByVal name As LongPtr, ByRef val As Variant) As Long Private Declare PtrSafe Function GetLongPropU Lib "StrokeScribeDL.dll" _ (ByVal name As LongPtr, ByRef val As Long) As Long Private Declare PtrSafe Function SavePictureU Lib "StrokeScribeDL.dll" _ (ByVal filename As LongPtr, ByVal format As Long, ByVal w As Long, ByVal h As Long, _ ByVal dpi As Long) As Long Private Declare PtrSafe Function GetZebraBitsU Lib "StrokeScribeDL.dll" _ (ByVal zebra As LongPtr, ByRef size As Long) As Long Private Declare PtrSafe Function GetFontOutU Lib "StrokeScribeDL.dll" _ (ByVal out As LongPtr, ByRef size As Long) As Long Private Declare PtrSafe Function GetPictureArray Lib "StrokeScribeDL.dll" _ (ByRef arr As Variant, ByVal format As Long, ByVal w As Long, ByVal h As Long, _ ByVal dpi As Long) As Long #End If #Else ' Excel XP or 2003 - only 32-bit VBA applications. Private Declare Function Initialize Lib "StrokeScribeDL.dll" () As Long Private Declare Function GetVer Lib "StrokeScribeDL.dll" _ (ByRef major As Long, ByRef major2 As Long, ByRef minor As Long) As Long Private Declare Function SetLongPropU Lib "StrokeScribeDL.dll" _ (ByVal name As Long, ByVal val As Long) As Long Private Declare Function SetDoublePropU Lib "StrokeScribeDL.dll" _ (ByVal name As Long, ByVal val As Double) As Long Private Declare Function SetTextPropU Lib "StrokeScribeDL.dll" _ (ByVal name As Long, ByVal val As Long) As Long Private Declare Function SetVariantPropU Lib "StrokeScribeDL.dll" _ (ByVal name As Long, ByRef val As Variant) As Long Private Declare Function GetLongPropU Lib "StrokeScribeDL.dll" _ (ByVal name As Long, ByRef val As Long) As Long Private Declare Function SavePictureU Lib "StrokeScribeDL.dll" _ (ByVal fileName As Long, ByVal format As Long, ByVal w As Long, ByVal h As Long, _ ByVal dpi As Long) As Long Private Declare Function GetZebraBitsU Lib "StrokeScribeDL.dll" _ (ByVal zebra As Long, ByRef size As Long) As Long Private Declare Function GetFontOutU Lib "StrokeScribeDL.dll" _ (ByVal out As Long, ByRef size As Long) As Long Private Declare Function GetPictureArray Lib "StrokeScribeDL.dll" _ (ByRef arr As Variant, ByVal format As Long, ByVal w As Long, ByVal h As Long, _ ByVal dpi As Long) As Long #End If ' Look for more barcode type definitions here Private Const CODE128A = 0 Private Const CODE128B = 1 Private Const CODE128C = 2 Private Const EAN13 = 3 Private Const CODE128 = 5 Private Const PDF417 = 6 Private Const DATAMATRIX = 8 Private Const EAN128 = 17 Private Const QRCODE = 25 Private Const GS1DATAMATRIX = 30 Private Const AZTEC = 33 ' Picture type definitions for SavePictureU() Private Const BMP = 0 Private Const GIF = 1 Private Const PNG = 2 Private Const JPG = 3 Private Const EMF = 4 Private Const TIFF = 5 Private Const BMP24 = 6 Private Const WMF = 7 Private Sub barcode() Dim wsh As Worksheet Set wsh = ActiveSheet Dim rc As Long rc = Initialize() If rc <> 0 Then Debug.Print "Initialize() failed, error code = " & rc Exit Sub End If SetLongPropU StrPtr("Alphabet"), QRCODE ' or DATAMATRIX or PDF417 or CODE128 Dim text As String text = "ABCDE3457" ' Paste your own barcode text here rc = SetTextPropU(StrPtr("Text"), StrPtr(text)) If rc > 0 Then Debug.Print "SetTextProp() failed, error code: " & rc Exit Sub End If Dim pic_path As String pic_path = Environ("TEMP") & "\barcode.wmf" ' A square barcode picture, 100x100 TWIPS (1440 TWIPS per inch) rc = SavePictureU(StrPtr(pic_path), WMF, 100, 100, 0) If rc > 0 Then Debug.Print "SavePicture() failed, error code: " & rc Exit Sub End If On Error Resume Next ' This deletes a previously created barcode shape if you have one wsh.Shapes("barcode").Delete On Error GoTo 0 Dim shp As Shape Set shp = wsh.Shapes.AddPicture(pic_path, msoFalse, msoTrue, 1, 1, _ Application.CentimetersToPoints(3), Application.CentimetersToPoints(3)) shp.name = "barcode" Kill pic_path End Sub

More examples

For more examples, visit the Programming examples section in the DLL documentation.