Insert Conditional Text in a Word Table Cell by Character Count
Question details
Add “...more” to the next table cell when generated text exceeds 45 characters.

- 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.
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.
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.
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.
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
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.
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”.

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.

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.




