VBA & Macro Problems

Insert Conditional Text in a Word Table Cell by Character Count

Maira MehtabMaira Mehtab Nov 22, 2026 869 views

Question details

Add “...more” to the next table cell when generated text exceeds 45 characters.

Insert Conditional Text in a Word Table Cell by Character Count
Product
Microsoft Word 2010
Device & OS
not provided
Scenario
A document-generation system replaces a token in a Word table cell with actual content after the template is uploaded.
Observed behavior
The source cell contains a token until generation, so the character count must be checked after the system populates the document.
Before you start

Identify the source cell and the next cell in the generated document, and confirm that the destination cell can be overwritten. Check whether the generating system preserves macros and allows a macro-enabled document to be opened or processed after token replacement.

Solution 1Recommended

Run a Word VBA macro after the token is replaced

Use this method when the generated document can run a macro after its table data has been populated.

The example checks the first cell in the first row of the first table and writes to the second cell. Adjust the table, row, and cell numbers to match your document. It assumes a regular, unmerged row and replaces any existing destination-cell content.

1
Open the VBA editor

In Word 2010, open the template or generated document and press Alt+F11. In the VBA editor, select the document project and choose Insert > Module.

2
Add the macro

Paste this code into the module: Sub AddMoreIfNeeded() Dim sourceText As String Dim targetCell As Cell If ActiveDocument.Tables.Count = 0 Then Exit Sub If ActiveDocument.Tables(1).Rows(1).Cells.Count < 2 Then Exit Sub sourceText = ActiveDocument.Tables(1).Cell(1, 1).Range.Text sourceText = Left$(sourceText, Len(sourceText) - 2) Set targetCell = ActiveDocument.Tables(1).Cell(1, 2) If Len(sourceText) > 45 Then targetCell.Range.Text = "...more" Else targetCell.Range.Text = "" End If End Sub

3
Save and generate the document

Choose File > Save As and select Word Macro-Enabled Document (*.docm). Generate a document through the external system and verify that the token has been replaced and the macro is still present.

4
Run and test the macro

Open the generated document, enable macros only if the file is trusted, and choose View > Macros > View Macros. Select AddMoreIfNeeded and click Run. Test text of 45 and 46 characters: only the 46-character case should insert “...more”.

Run a Word VBA macro after the token is replaced
Run timing matters: The macro removes Word’s two end-of-cell characters before counting. It does not run automatically after generation; the generated file must retain the macro, and a user or a supported automation process must run it after token replacement.
Free Microsoft Office alternative

Edit Word Table Documents with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its Microsoft Word format compatibility can help you move existing documents across, but it does not resolve Word 2010 macro execution or guarantee that a VBA macro will run unchanged.

Edit table-cell text in a familiar document interfaceOpen and edit Microsoft Word DOC and DOCX filesUse a free, lightweight office suiteMove existing Word documents into a familiar editing workflow
microsoft office alternative - wps office

Frequently Asked Questions

Why should the macro run after document generation?

Before generation, the cell contains the token rather than the final text. Run the macro after the external system replaces the token so Len counts the populated content.

Does a cell with exactly 45 characters get the marker?

No. The condition Len(sourceText) > 45 inserts “...more” only when the count is greater than 45. Exactly 45 characters clears the destination cell.

How do I point the macro to a different cell?

In the VBA editor, change Tables(1) and the row and column numbers in Cell(1, 1) and Cell(1, 2). The example assumes the source and destination are adjacent cells in a regular, unmerged row.

What if the generated document does not retain the macro?

Check the generation system’s output settings and whether it supports macro-enabled DOCM files. If it removes macros or cannot run them after generation, use a supported post-generation automation process or implement the check in the generating system.