I would like to generate a 2d barcode (PDF417 or QR codes) in an Excel cell using macros. Just wondering is there any free alternatives to paid libraries to do this?
I know certain tools can do the job but it is relatively expensive to us.
I would like to generate a 2d barcode (PDF417 or QR codes) in an Excel cell using macros. Just wondering is there any free alternatives to paid libraries to do this?
I know certain tools can do the job but it is relatively expensive to us.
The VBA module barcode-vba-macro-only (mentioned by Sébastien Ferry in the comments) is a pure VBA 1D/2D code generator created by Jiri Gabriel under MIT License in 2013.
The code isn't completely simple to understand, but many comments have been translated from Czech to English in the version linked above.
To use it in a worksheet, just copy or import barcody.bas into your VBA in a module. In a worksheet, put in the function like this:
=EncodeBarcode(CELL("SHEET"),CELL("ADDRESS"),A2,51,1,0,2)
The usage is as follows:
CELL("SHEET)
and CELL("ADDRESS")
as they are since it's just giving reference to the worksheet and cell address you have the formula I added wrapper functions to make it a pure VBA function call rather than using it as a formula in a worksheet:
Public Sub RenderQRCode(workSheetName As String, cellLocation As String, textValue As String) Dim s_param As String Dim s_encoded As String Dim xSheet As Worksheet Dim QRShapeName As String Dim QRLabelName As String s_param = "mode=Q" s_encoded = qr_gen(textValue, s_param) Call DrawQRCode(s_encoded, workSheetName, cellLocation) Set xSheet = Worksheets(workSheetName) QRShapeName = "BC" & "$" & Left(cellLocation, 1) _ & "$" & Right(cellLocation, Len(cellLocation) - 1) & "#GR" QRLabelName = QRShapeName & "_Label" With xSheet.Shapes(QRShapeName) .Width = 30 .Height = 30 End With On Error Resume Next If Not (xSheet.Shapes(QRLabelName) Is Nothing) Then xSheet.Shapes(QRLabelName).Delete End If xSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, _ xSheet.Shapes(QRShapeName).Left+35, _ xSheet.Shapes(QRShapeName).Top, _ Len(textValue) * 6, 30) _ .Name = QRLabelName With xSheet.Shapes(QRLabelName) .Line.Visible = msoFalse .TextFrame2.TextRange.Font.Name = "Arial" .TextFrame2.TextRange.Font.Size = 9 .TextFrame.Characters.Text = textValue .TextFrame2.VerticalAnchor = msoAnchorMiddle End With End Sub Sub DrawQRCode(xBC As String, workSheetName As String, rangeName As String, Optional xNam As String) Dim xShape As Shape, xBkgr As Shape Dim xSheet As Worksheet Dim xRange As Range, xCell As Range Dim xAddr As String Dim xPosOldX As Double, xPosOldY As Double Dim xSizeOldW As Double, xSizeOldH As Double Dim x, y, m, dm, a As Double Dim b%, n%, w%, p$, s$, h%, g% Set xSheet = Worksheets(workSheetName) Set xRange = Worksheets(workSheetName).Range(rangeName) xAddr = xRange.Address xPosOldX = xRange.Left xPosOldY = xRange.Top xSizeOldW = 0 xSizeOldH = 0 s = "BC" & xAddr & "#GR" x = 0# y = 0# m = 2.5 dm = m * 2# a = 0# p = Trim(xBC) b = Len(p) For n = 1 To b w = AscL(Mid(p, n, 1)) Mod 256 If (w >= 97 And w = 97 And w 0 Then xShape.Width = xSizeOldW xShape.Height = xSizeOldH End If Else If Not (xBkgr Is Nothing) Then xBkgr.Delete End If Exit Sub fmtxshape: xShape.Line.Visible = msoFalse xShape.Line.Weight = 0# xShape.Fill.Solid xShape.Fill.ForeColor.RGB = RGB(0, 0, 0) g = g + 1 xShape.Name = "BC" & xAddr & "#BR" & g If g = 1 Then xSheet.Shapes.Range(Array(xBkgr.Name, xShape.Name)).Group.Name = s Else xSheet.Shapes.Range(Array(s, xShape.Name)).Group.Name = s End If Return End Sub
With this wrapper, you can now simply call to render QRCode by calling this in VBA:
Call RenderQRCode("Sheet1", "A13", "QR Value")
Just input the worksheet name, cell location, and the QR_value. The QR shape will get drawn at the location you specified.
You can play around with this section of the code to change the size of the QR
With xSheet.Shapes(QRShapeName) .Width = 30 'change your size .Height = 30 'change your size End With
I know this is quite an old and well-established post (though the very good existing answer has not been accepted yet), but I would like to share an alternative that I prepared for a similar post in StackOverflow in Portuguese using the free online API from QR Code Generator.
The code is the following:
Sub GenQRCode(ByVal data As String, ByVal color As String, ByVal bgcolor As String, ByVal size As Integer) On Error Resume Next For i = 1 To ActiveSheet.Pictures.Count If ActiveSheet.Pictures(i).Name = "QRCode" Then ActiveSheet.Pictures(i).Delete Exit For End If Next i sURL = "https://api.qrserver.com/v1/create-qr-code/?" + "size=" + Trim(Str(size)) + "x" + Trim(Str(size)) + "&color=" + color + "&bgcolor=" + bgcolor + "&data=" + data Debug.Print sURL Set pic = ActiveSheet.Pictures.Insert(sURL + sParameters) Set cell = Range("D9") With pic .Name = "QRCode" .Left = cell.Left .Top = cell.Top End With End Sub
It gets the job done by simply (re)creating an image from the URL built from the parameters in the cells. Naturally, the user must be connected to the Internet.
For example (the worksheet, with contents in Brazilian Portuguese, can be downloaded from 4Shared):