# Build and Upload a DOCX Template

Use this page when you want Word-native design in Microsoft Word and a generated `.docx` output from Salesforce data.

A DOCX template is built in Word, then uploaded to S-Docs for generation.

Use DOCX when Word editing matters more than HTML layout or slide design.

{% hint style="info" %}
Pick **DOCX** when you need editable Word output, Word design tools, and bracket-based S-Docs syntax.

Pick [PowerPoint (PPTX)](/sdocs/template-architecture/document-formats/pptx-format.md) when you need slide-based output.
{% endhint %}

### Before you start

Make sure you have:

* a new S-Docs template with format set to **DOCX**
* the correct **Related To Type** for your base record
* a `.docx` file you can edit in Microsoft Word
* permission to upload the latest Word file into **Template Editor**

{% hint style="warning" %}
DOCX templates use the uploaded Word file.

If you change the file in Word, re-upload it before testing again.
{% endhint %}

### Build and upload a DOCX template

{% stepper %}
{% step %}

### Create the template record

Create a new S-Docs template.

Set **Template Format** to `DOCX`.
{% endstep %}

{% step %}

### Build the document in Microsoft Word

Create the document layout in Word exactly as you want it to appear.

Use normal Word tools for fonts, spacing, tables, headers, images, and page layout.
{% endstep %}

{% step %}

### Add S-Docs merge syntax

Insert merge fields, render blocks, and query output using bracketed Microsoft template syntax.

{% code title="docx-basic-syntax.txt" %}

```plaintext
[{{!Opportunity.Name}}]

[<!--RENDER='{{!Opportunity.StageName}}' == 'Closed Won' -->]
  Approved amount: [{{!Opportunity.Amount #,###.00}}]
[<!--ENDRENDER-->]
```

{% endcode %}
{% endstep %}

{% step %}

### Add table-based repeating data when needed

If you need a related list, build it inside a Word table.

Your Word table needs:

1. a header row
2. an odd row with placeholder text
3. an even row with placeholder text

Use the Microsoft template table wrapper around that table.
{% endstep %}

{% step %}

### Upload the `.docx` file

Open the template in **Template Editor**.

Upload the Word file so S-Docs can use it for generation.
{% endstep %}

{% step %}

### Generate and test with real data

Test the template with real Salesforce records.

Verify merge fields, table output, images, conditional sections, and page layout before rollout.
{% endstep %}
{% endstepper %}

### Core DOCX syntax patterns

#### Merge fields

Wrap merge fields in square brackets.

{% code title="docx-merge-fields.txt" %}

```plaintext
[{{!Account.Name}}]
[{{!Opportunity.Amount #,###.00}}]
```

{% endcode %}

Use this same pattern for relationship fields too.

{% code title="docx-merge-field-paths.txt" %}

```plaintext
[{{!Opportunity.Name}}]
[{{!Opportunity.Account.Name}}]
[{{!Contact.Email}}]
```

{% endcode %}

#### Conditional logic

Wrap both `RENDER` tags in square brackets.

{% code title="docx-render-block.txt" %}

```plaintext
[<!--RENDER='{{!Contact.Phone}}' != 'NULL' -->]
  Phone: [{{!Contact.Phone}}]
[<!--ENDRENDER-->]
```

{% endcode %}

Keep merge fields inside rendered content in square brackets too.

Use the same operators you use in standard conditional logic.

{% code title="docx-render-operators.txt" %}

```plaintext
[<!--RENDER=( ('{{!Account.BillingState}}' == 'CA' || '{{!Account.BillingState}}' == 'NV')
             && '{{!Account.Active_Contract__c}}' == 'True' ) -->]
  Regional account with active contract
[<!--ENDRENDER-->]
```

{% endcode %}

Nested render blocks work too.

{% code title="docx-render-nested.txt" %}

```plaintext
[<!--RENDER='{{!Opportunity.StageName}}' == 'Closed Won' -->]
  Congratulations on your purchase.
  [<!--RENDER1='{{!Opportunity.Install_Date__c}}' != 'NULL' -->]
    Installation is scheduled for [{{!Opportunity.Install_Date__c}}].
  [<!--ENDRENDER1-->]
[<!--ENDRENDER-->]
```

{% endcode %}

#### Named queries

Wrap both the query block and output fields in square brackets.

{% code title="docx-named-query.txt" %}

```plaintext
[{{!<lineitemsSOQL>
  <class>none</class>
  <queryname>TopItem</queryname>
  <soql>
    SELECT Name, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
    ORDER BY TotalPrice DESC
    LIMIT 1
  </soql>
</lineitemsSOQL>}}]

[{{!TopItem.Name}}]
[{{!TopItem.TotalPrice #,###.00}}]
```

{% endcode %}

If you need a filtered or offset result, keep the same square-bracket wrapping.

{% code title="docx-named-query-filter-offset.txt" %}

```plaintext
[{{!<lineitemsSOQL>
  <class>none</class>
  <queryname>FilteredItems</queryname>
  <soql>
    SELECT Name, TotalPrice
    FROM OpportunityLineItem
    WHERE OpportunityId = '{{!Opportunity.Id}}'
    ORDER BY TotalPrice DESC
  </soql>
  <filter id="1">TotalPrice >= 1000</filter>
</lineitemsSOQL>}}]

[{{!FilteredItems.Name filter="1" offset="1"}}]
[{{!FilteredItems.TotalPrice filter="1" offset="1" #,###.00}}]
```

{% endcode %}

#### Related list tables

DOCX related lists render inside Word tables.

Use the same table wrapper pattern used in Microsoft templates.

{% 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 %}

Use this structure exactly:

1. Put the opening block before the Word table.
2. Keep the table inside the `tableformat` wrapper.
3. Put the query and `<column>` tags after the table.
4. Make sure each `<column>` matches a field returned in `SELECT`.

#### Component templates

DOCX supports DOCX component templates.

Wrap the component reference in square brackets too.

{% code title="docx-component.txt" %}

```plaintext
[{{{{!Component Template Name}}}}]
```

{% endcode %}

{% hint style="info" %}
For DOCX syntax, five rules matter most:

* merge fields use square brackets
* named query output fields use square brackets
* `RENDER` blocks use square brackets
* merge fields inside rendered content also use square brackets
* component references use square brackets
  {% endhint %}

### Common syntax mistakes

* **Field prints as text:** add square brackets around the merge field
* **Conditional block does not run:** wrap both opening and closing `RENDER` tags in square brackets
* **Named query returns nothing:** confirm the output field is wrapped in square brackets and included in `SELECT`
* **Related list table breaks:** confirm the Word table has a header row, an odd row, and an even row
* **DOCX generation fails:** clear Word spelling and grammar markup, then re-upload the file

### What DOCX supports

DOCX templates support these common S-Docs capabilities:

* merge fields
* conditional logic
* named queries
* related list tables
* component templates
* images and Word-native formatting

### Important DOCX considerations

Keep these DOCX-specific rules in mind:

* Live Edit is not supported.
* DOCX formatting comes from Word, not CSS-driven page markup.
* Related list tables must be built as Word tables.
* You must re-upload the file after each Word change.
* Word proofing markup can break generation.

{% hint style="warning" %}
If DOCX generation fails with `spellStart`, `gramStart`, or `gramEnd`, clear all red and blue proofing marks in Word and upload the cleaned file again.
{% endhint %}

### Next reads by goal

* Need a DOCX overview? Read [Microsoft Word (DOCX)](/sdocs/template-architecture/document-formats/docx-s-docs.md).
* Need DOCX setup considerations? Read [DOCX Rendering Considerations](/sdocs/template-architecture/document-formats/docx-s-docs/docx-rendering-considerations.md).
* Need merge field syntax? Read [Merge Fields With Microsoft Templates (DOCX, PPTX, XLSX)](/sdocs/template-architecture/inserting-merge-fields/merge-fields-with-microsoft-templates-docx-pptx-xlsx.md).
* Need table setup examples? Read [How to: Create a Table In Microsoft Templates (DOCX, PPTX)](/sdocs/advanced-template-logic/related-lists/how-to-create-a-table-in-microsoft-templates-docx-pptx.md).
* Need conditional syntax? Read [Quick Setup: Conditional Logic With Microsoft Templates (DOCX, PPTX, XLSX)](/sdocs/advanced-template-logic/conditional-logic/quick-setup-conditional-logic-with-microsoft-templates-docx-pptx-xlsx.md).
* Need DOCX components? Read [Component (DOCX)](/sdocs/advanced-template-logic/build-reusable-templates/component-template-how-to-article/component-docx.md).
* Need to fix proofreading markup errors? Read [DOCX Generation Failure: SpellStart, GramStart, or GramEnd](/sdocs/template-architecture/document-formats/docx-s-docs/additional-resources/docx-generation-failure-spellstart.md).
* Need attached total rows? Read [How to Add a Subtotal Row to a DOCX Table](/sdocs/template-architecture/document-formats/docx-s-docs/additional-resources/add-a-subtotal-line-to-the-end-of-a-table-in-docx.md).


---

# 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/template-architecture/document-formats/docx-s-docs/build-and-upload-a-docx-template.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.
