VBA & Macro Problems

Populate an Excel Worksheet from Word Content Controls Using VBA

Huma Ashraf ChHuma Ashraf Ch Nov 14, 2026 870 views

Question details

Transfer values from Word form content controls into an Excel worksheet, while handling existing records and forms stored in different folders.

Populate an Excel Worksheet from Word Content Controls Using VBA
Product
Microsoft Excel and Microsoft Word
Device & OS
not provided
Scenario
Users complete Word forms stored in separate folders and need their data collected in a known Excel workbook.
Observed behavior
The desired workflow adds new records or updates existing ones without creating unintended duplicates or conflicting with an open workbook.
Before you start

For the example below, use desktop Excel and Word on Windows, and create an Excel sheet named Forms with headers RecordID, Name, and Email in cells A1:C1. In each Word form, assign the content controls the tags RecordID, Name, and Email, and give each form a unique, persistent RecordID.

Solution 1Recommended

Import Word Forms from Excel and Update Rows by Record ID

Use this approach to select forms from different folders and let Excel add or update rows based on a stable identifier.

The macro opens selected Word documents read-only and matches each RecordID against column A. A matching row is updated; a new ID is appended. Moving or renaming a form does not change its identity as long as its RecordID stays the same.

This example requires exactly one content control for each of the three tags. It skips a form if a tag is missing or repeated, its RecordID is blank, or its ID matches more than one existing row.

1
Prepare the workbook

In Excel, create the Forms sheet and headers described above. Check that existing IDs in column A are unique, then use File > Save As to save the workbook as an Excel Macro-Enabled Workbook (.xlsm).

2
Add the import macro

Press Alt+F11 in Excel, choose Insert > Module, and paste this code: Option Explicit Sub ImportWordForms() Dim sh As Worksheet, picker As Object Dim wordApp As Object, doc As Object, controls As Object Dim file As Variant, tags As Variant, values(0 To 2) As String Dim i As Long, r As Long, lastRow As Long, targetRow As Long Dim matches As Long, imported As Long, skipped As Long Dim valid As Boolean, key As String On Error GoTo Failed Set sh = ThisWorkbook.Worksheets("Forms") Set picker = Application.FileDialog(3) With picker .Title = "Select Word forms" .AllowMultiSelect = True .Filters.Clear .Filters.Add "Word documents", "*.docx;*.docm" If .Show <> -1 Then Exit Sub End With Set wordApp = CreateObject("Word.Application") wordApp.Visible = False wordApp.AutomationSecurity = 3 tags = Array("RecordID", "Name", "Email") For Each file In picker.SelectedItems Set doc = wordApp.Documents.Open(CStr(file), False, True, False) valid = True For i = 0 To 2 Set controls = doc.SelectContentControlsByTag(CStr(tags(i))) If controls.Count <> 1 Then valid = False Exit For End If values(i) = Trim$(controls.Item(1).Range.Text) Next i doc.Close False Set doc = Nothing If valid Then key = values(0) If Len(key) = 0 Then valid = False End If If valid Then lastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row matches = 0 targetRow = 0 For r = 2 To lastRow If StrComp(Trim$(CStr(sh.Cells(r, 1).Value2)), key, vbTextCompare) = 0 Then matches = matches + 1 targetRow = r End If Next r If matches > 1 Then valid = False ElseIf matches = 0 Then targetRow = lastRow + 1 End If End If If valid Then sh.Range(sh.Cells(targetRow, 1), sh.Cells(targetRow, 3)).NumberFormat = "@" For i = 0 To 2 sh.Cells(targetRow, i + 1).Value2 = values(i) Next i imported = imported + 1 Else skipped = skipped + 1 End If Next file MsgBox imported & " form(s) imported; " & skipped & " skipped. Review the sheet and save the workbook." GoTo Cleanup Failed: MsgBox "Import stopped: " & Err.Description & ". Review any rows already changed." Cleanup: On Error Resume Next If Not doc Is Nothing Then doc.Close False If Not wordApp Is Nothing Then wordApp.Quit End Sub

3
Run and review the import

In Excel, press Alt+F8, select ImportWordForms, and click Run. Select the Word forms in the file picker; run the macro again for forms in other folders. Review the Forms sheet and press Ctrl+S to save the changes.

Import Word Forms from Excel and Update Rows by Record ID
Workbook access: Run the import in the writable copy of the workbook. If Excel opens it read-only because another user has it locked, coordinate access or save changes to a separate copy before merging them.
Free Microsoft Office alternative

Review Word Forms and Excel Data with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with documents and spreadsheets. Its Microsoft Office format compatibility helps you move existing files into WPS, but it does not fix or replace the Microsoft Office VBA automation described above.

Review form documents and worksheet data in one office suiteOpen and edit common Microsoft Word and Excel formats, including DOCX and XLSXUse a familiar interface when moving from Microsoft OfficeWork with a free, lightweight office alternative
microsoft office alternative - wps office

Frequently Asked Questions

How does the macro avoid adding a duplicate row?

It compares the form's RecordID with the IDs in column A of the Forms sheet. One match is updated, no match creates a new row, and multiple matches cause the form to be skipped.

Can I import forms stored in different folders?

Yes. Run ImportWordForms from Excel's Alt+F8 macro dialog and select the forms in one folder. Repeat the import for each additional folder.

Where do I set a Word content-control tag?

In Word, enable the Developer tab if needed, select the content control, and choose Developer > Properties. Enter its tag, such as RecordID, Name, or Email.

Does the import macro save the workbook automatically?

No. It changes the open workbook so you can review the Forms sheet first. Press Ctrl+S afterward to save the changes.

What happens if the workbook is already open by someone else?

Excel may open the workbook read-only or report that it is locked. Use a writable copy or coordinate access before saving; the import macro does not resolve multiuser locking.