# How to: Create a Table In Microsoft Templates (DOCX, PPTX)

Use this pattern when you need a repeating table in a DOCX template.

The SOQL logic stays the same.

The table wrapper changes.

### What DOCX needs

A DOCX related list table needs three rows in Microsoft Word:

1. A header row
2. An odd row with placeholder text
3. An even row with placeholder text

S-Docs uses the odd and even rows as the repeating pattern.

### Basic pattern

{% code title="docx-related-list-table.txt" %}

```plaintext
[{{!<lineitemsSOQL>
<tableformat>]

| Product | Quantity | Total |
| sample   | sample   | sample |
| sample   | sample   | sample |

[</tableformat>
<soql>
  SELECT Product2.Name, Quantity, TotalPrice
  FROM OpportunityLineItem
  WHERE OpportunityId = '{{!Opportunity.Id}}'
  ORDER BY SortOrder
</soql>
<column>Product2.Name</column>
<column>Quantity</column>
<column format-number="#,##0.00" prefix="$">TotalPrice</column>
</lineitemsSOQL>}}]
```

{% endcode %}

### Build it step by step

{% stepper %}
{% step %}

### Create the table in Word

Insert a table with the number of columns you want to display.

Use the first row for visible headers.

Use the second and third rows as sample rows for styling.
{% endstep %}

{% step %}

### Add the opening tags above the table

Place this block before the table:

{% code title="opening-tags.txt" %}

```plaintext
[{{!<lineitemsSOQL>
<tableformat>]
```

{% endcode %}

This tells S-Docs to treat the Word table as the output format for the related list.
{% endstep %}

{% step %}

### Add the closing tags and query below the table

Place this block after the table:

{% code title="closing-tags-and-query.txt" %}

```plaintext
[</tableformat>
<soql>
  SELECT Product2.Name, Quantity, TotalPrice
  FROM OpportunityLineItem
  WHERE OpportunityId = '{{!Opportunity.Id}}'
  ORDER BY SortOrder
</soql>
<column>Product2.Name</column>
<column>Quantity</column>
<column>TotalPrice</column>
</lineitemsSOQL>}}]
```

{% endcode %}

`<soql>` defines the rows.

Each `<column>` defines one output column.
{% endstep %}

{% step %}

### Match columns to the query

Each `<column>` must match a field returned in `SELECT`.

Keep the column order aligned with the visible header order.

If you use relationship fields, use dot notation in both places.
{% endstep %}
{% endstepper %}

### Example for Opportunity Products

Use this pattern to show products from an Opportunity in a DOCX table.

{% code title="opportunity-products-docx.txt" %}

```plaintext
[{{!<lineitemsSOQL>
<tableformat>]

| Product | Quantity | Sales Price |
| sample   | sample   | sample      |
| sample   | sample   | sample      |

[</tableformat>
<soql>
  SELECT Product2.Name, Quantity, UnitPrice
  FROM OpportunityLineItem
  WHERE OpportunityId = '{{!Opportunity.Id}}'
  ORDER BY SortOrder
</soql>
<column>Product2.Name</column>
<column>Quantity</column>
<column format-number="#,##0.00" prefix="$">UnitPrice</column>
</lineitemsSOQL>}}]
```

{% endcode %}

### Common fixes

* **Syntax prints as text:** Use square brackets around the full block in DOCX.
* **No rows returned:** Check the `WHERE` clause against the template base record.
* **Blank column:** Add that field to `SELECT` and to a matching `<column>`.
* **Table does not repeat correctly:** Make sure the Word table has header, odd, and even rows inside the `tableformat` block.
* **Wrong values in a column:** Match the `<column>` name to the queried field or alias exactly.

{% hint style="warning" %}
Build and test the query first.

Then place it inside the DOCX table wrapper.
{% endhint %}

### Keep going

* Review [Related List Structure (DOCX)](broken://spaces/WKNnJmhJBQwhdk5WBFsi/pages/47d7aeb4bb2b144961088cfdf431dc82e5549990) for the DOCX-specific wrapper rules
* Review [Introduction to SOQL](broken://spaces/WKNnJmhJBQwhdk5WBFsi/pages/xUG0FooN8svjvhtdVSzU) for query syntax
* Review [Related List Attributes](broken://spaces/WKNnJmhJBQwhdk5WBFsi/pages/vkKv4e3eEWaiLFASBjO6) for formatting options like number and date output


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sdocs.com/sdocs/advanced-template-logic/related-lists/how-to-create-a-table-in-microsoft-templates-docx-pptx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
