How to Insert a Word Caption Inside a Table Cell with VBA
Question details
The user needs to insert a text caption and an auto-numbered field directly inside a specific Word table cell using VBA.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting where charts and captions need to be dynamically inserted into specific table cells via VBA macros.
- Observed behavior
- When executing the macro, the inserted captions appear outside of the targeted table cells because the default range includes the end-of-cell marker.
Ensure that the Developer tab is enabled in your Word application so you can access the Visual Basic Editor (ALT + F11).
Define a Cell-Specific Range and Exclude the End-of-Cell Marker
Create a Range object targeting the exact table cell, then manually adjust the range's end point to exclude the end-of-cell marker before inserting the SEQ field and text.
When you reference a table cell's range in VBA, the range inherently includes the end-of-cell marker. If you collapse this range to the end and insert text or fields, Word will push your newly inserted content outside the table cell. To fix this, you must mathematically subtract 1 from the range's End property before making your insertion.
Open the VBA Editor and set up your variables: 'Dim tblNew As Table' and 'Dim rng As Range'.
After adding your table, assign the range to the desired cell. For example: 'Set rng = tblNew.Cell(1, 1).Range'.
Add your initial caption text (like 'Table ') by setting 'rng.Text = "Table "'.
Reset the range to the cell, adjust the start position past your prefix text (e.g., 'rng.Start = rng.Start + 6'), collapse the range using 'wdCollapseStart', and insert the sequential field: '.Fields.Add rng, wdFieldEmpty, "SEQ Table \\* ARABIC"'.
This is the critical step. Reset the range to the cell again, and subtract 1 from the end: 'rng.End = rng.End - 1'. Collapse it to the end ('wdCollapseEnd') and use 'rng.InsertBefore " Your Caption Text"' to ensure it stays strictly within the cell boundaries.
Finally, reset the range to the whole cell and apply the correct style: 'rng.Style = "Caption"'.

Automate Complex Tables with WPS Writer
WPS Office offers comprehensive compatibility with Microsoft Word documents, including powerful macro support to automate repetitive tasks like inserting tables, charts, and captions with precision.
- 1. Enable the Developer Tools: Open WPS Writer, go to the settings, and ensure your Developer tab is visible to access macro functionalities.
- 2. Open the Macro Editor: Click on 'Macros' under the Developer tab to open the built-in VBA or JS macro editor.
- 3. Write or Paste the Script: Paste your custom table and caption scripting code directly into a new module.
- 4. Run and Automate: Execute your script to automatically generate tables with perfectly aligned, auto-numbered captions inside the cells.

Frequently Asked Questions
Why does text inserted via VBA sometimes appear completely outside the target Word table?
This happens when the targeted Range in VBA includes the hidden end-of-cell marker. If you insert text at the very end of this default range, Word treats the insertion point as being outside the cell. You must explicitly adjust the range end (e.g., rng.End = rng.End - 1) before inserting.
How do I insert an auto-numbering SEQ field using VBA?
You can insert a SEQ field by utilizing the Fields.Add method on a Range object. For example: ActiveDocument.Fields.Add Range:=rng, Type:=wdFieldEmpty, Text:="SEQ Table \* ARABIC". This dynamically generates a sequential number for your caption.
Can I format the cell contents as a 'Caption' after inserting it?
Yes. Once the text and fields are successfully inserted into the cell, you can re-target the cell's range and apply the built-in Word style using the command: rng.Style = "Caption".




