Document Editing Problems

Create a QR Code in Microsoft Word from Form Fields Using VBA

Maira MehtabMaira Mehtab Nov 22, 2026 868 views

Question details

Combine values entered in Word document fields and insert them into a QR code using VBA and DISPLAYBARCODE.

Create a QR Code in Word from Form Fields Using VBA
Product
Microsoft Word
Device & OS
not provided
Scenario
A document contains fields that users fill out, and their values need to be encoded together beside the form.
Observed behavior
The desired result is a scannable QR code containing the combined field values.
Before you start

Identify the type and names of the document fields. The example below uses three plain-text content controls titled Field1, Field2, and Field3 in a desktop version of Word that supports VBA and DISPLAYBARCODE.

Solution 1Recommended

Combine content control values and insert a QR code with VBA

Use this method when the form fields are plain-text content controls with unique titles.

The macro joins the three values with a pipe character and inserts a DISPLAYBARCODE field at the cursor. Change the delimiter if the receiving system expects a different format.

Legacy form fields, ActiveX controls, and drawing text boxes require different VBA references; the content control references in this example will not read them.

1
Name the content controls

In Word, select each plain-text content control, open Developer > Properties, and set its Title to Field1, Field2, or Field3. Use each title only once.

2
Add the macro

Open Developer > Visual Basic, choose Insert > Module, and paste this code: Sub InsertFormQRCode() Dim names As Variant, values(0 To 2) As String Dim matches As ContentControls Dim payload As String, i As Long Dim insertAt As Range, barcode As Field names = Array("Field1", "Field2", "Field3") For i = 0 To 2 Set matches = ActiveDocument.SelectContentControlsByTitle(names(i)) If matches.Count <> 1 Then MsgBox "Expected one content control titled " & names(i) Exit Sub End If values(i) = matches.Item(1).Range.Text If InStr(values(i), Chr(34)) > 0 Or InStr(values(i), vbCr) > 0 Or InStr(values(i), vbLf) > 0 Then MsgBox "Remove quotation marks and line breaks from " & names(i) Exit Sub End If Next i payload = values(0) & "|" & values(1) & "|" & values(2) Set insertAt = Selection.Range.Duplicate insertAt.Collapse Direction:=wdCollapseStart Set barcode = ActiveDocument.Fields.Add(Range:=insertAt, Type:=wdFieldEmpty, Text:="DISPLAYBARCODE " & Chr(34) & payload & Chr(34) & " QR", PreserveFormatting:=False) barcode.Update End Sub

3
Fill out the form

Return to the document and enter the intended values in Field1, Field2, and Field3. Avoid quotation marks and line breaks in this example.

4
Insert and test the code

Click where the QR code should appear next to the completed fields. Open Developer > Macros, select InsertFormQRCode, and click Run. Scan the resulting code to verify the combined values.

Combine content control values and insert a QR code with VBA
Regenerate after edits: The field contains a snapshot of the values when the macro runs. If a value changes, delete the old QR code and run the macro again at the desired location.
Free Microsoft Office alternative

Work with Word documents in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with Office files and moving everyday documents between applications. The Word VBA and DISPLAYBARCODE workflow above remains a Microsoft Word solution; test specialized fields and macros before relying on them in WPS.

Open and edit Word-compatible DOC and DOCX documents.Review and edit form documents in a familiar interface.Use a free, lightweight Office alternative for everyday document work.
microsoft office alternative - wps office

Frequently Asked Questions

How do I identify the fields used by the macro?

Select each content control in Word and open Developer > Properties. The example looks for the Title values Field1, Field2, and Field3, with exactly one control for each title.

Will the QR code update when someone changes a form value?

Not with this macro. It inserts the current values as literal field text. Delete the old code and run the macro again after changing the form.

Can I use this code with legacy form fields or drawing text boxes?

Not without changing how the values are read. The example uses SelectContentControlsByTitle, which applies to content controls rather than legacy form fields or drawing text boxes.

Why does the macro stop when a value contains a quotation mark?

The sample rejects quotation marks and line breaks to avoid producing an invalid DISPLAYBARCODE field. Remove those characters from the content control and run the macro again.