Office Settings & Configuration

How to Insert a QR Code into a Word Table Using VBA

Huda QurayshiHuda Qurayshi Nov 2, 2026 868 views

Question details

The user needs a reliable method to programmatically generate and place a QR code into a specific cell of a Word table using VBA.

How to Insert a QR Code into a Word Table Using VBA
Product
Word
Device & OS
not provided
Scenario
Automating document creation where dynamic QR codes must be placed in precise table locations without relying on external image generation URLs.
Observed behavior
External QR generation URLs produce errors in Word. The native DISPLAYBARCODE field works successfully at the current cursor selection, but targeting a specific table cell programmatically requires proper range manipulation.
Before you start

Ensure that the Developer tab is enabled in your ribbon and that your document is saved as a Macro-Enabled Document (.docm) to allow VBA scripts to run properly.

Solution 1Recommended

Use VBA Range to Insert DISPLAYBARCODE Field in a Specific Cell

Use the VBA Range object to target the exact table cell, collapse the range to ignore the end-of-cell marker, and insert the native DISPLAYBARCODE field.

Microsoft Word has a built-in field code called DISPLAYBARCODE that can generate QR codes offline. To insert this field into a specific table cell instead of the current cursor position, you must define the target cell as a Range, step back from the hidden end-of-cell marker, and then insert the field.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.

2
Create a New Module

In the Project Explorer panel, right-click on your document name, select 'Insert', and click 'Module' to open a blank coding window.

3
Insert the VBA Code

Copy and paste the following code into the module to target the first cell (Row 1, Column 1) of the first table: `Dim r As Range` `Set r = ActiveDocument.Tables(1).Cell(1, 1).Range` `r.End = r.End - 1` `r.Collapse wdCollapseStart` `ActiveDocument.Fields.Add r, wdFieldEmpty, "DISPLAYBARCODE ""Test"" QR \h1440", False`

4
Customize the Output

Replace the word "Test" in the code string with your desired text or URL. You can also adjust the table index or cell coordinates (Row, Column) to target different cells.

5
Execute the Macro

Press F5 while inside the VBA editor, or run the macro from the Developer tab in your Word document to generate the QR code inside the designated cell.

Use VBA Range to Insert DISPLAYBARCODE Field in a Specific Cell
Adjusting QR Code Size: The '\h1440' switch in the field formula sets the height of the QR code in twips (1440 twips equals 1 inch). You can increase or decrease this number to change the size of the generated QR code.

Automate Document Workflows Easily with WPS Writer

WPS Office provides robust VBA and macro support, making it simple to automate document tasks and manage table contents programmatically. For those who prefer a visual approach, WPS Writer also includes intuitive UI tools for document formatting and object insertion.

  1. 1. Open WPS Writer: Launch WPS Office and open your macro-enabled document (.docm).
  2. 2. Access the Developer Tools: Click on the Developer tab in the top ribbon and select 'Macro Editor' to open the VBA environment.
  3. 3. Run Your Script: Paste your table-targeting script into the module and execute it to place your QR code instantly.
  4. 4. Explore Visual Tools: Alternatively, explore the 'Insert' tab to manually add images, shapes, and other elements directly into your table cells without coding.
Fully compatible with Microsoft Word macro-enabled formats (.docm).Includes a built-in Macro Editor for creating and executing VBA scripts.Highly optimized for manipulating tables, ranges, and custom fields.Lightweight, fast, and completely free to download.
microsoft office alternative - wps office

Frequently Asked Questions

Why do external QR code URLs cause errors in Word?

Word's security settings often block dynamic external image links (like QRServer API URLs) to prevent external tracking or malicious code execution. Using the native DISPLAYBARCODE field avoids network requests and bypasses these security restrictions.

How do I dynamically change the data inside the QR code using VBA?

You can dynamically generate the QR code by concatenating a variable into the field code formula string. For example: `"DISPLAYBARCODE """ & myDataVariable & """ QR \h1440"`. This allows the macro to pull data from other cells or user input.

What is the purpose of the 'r.End = r.End - 1' line in the macro code?

Every table cell in Word contains a hidden end-of-cell marker. Subtracting 1 from the range end ensures your selection is placed inside the actual cell content area. Without this, Word may throw an error or place the field outside the intended formatting boundaries.