# Merge Fields With Microsoft Templates (DOCX, PPTX, XLSX)

## Inserting Merge Fields in DOCX Templates

Merge fields pull live Salesforce data into a DOCX template.

They act as placeholders. At generation time, S-Docs replaces them with record values.

### What a DOCX merge field looks like

Use this pattern:

```plaintext
[{{!Object.Field}}]
```

Examples:

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

DOCX templates require square brackets around the merge field.

DOCX also uses inline formatting syntax inside the field.

### How to add a merge field in a DOCX template

{% stepper %}
{% step %}

### Choose the record value

Identify the object and field you want to merge.

Example: `Opportunity.Name`
{% endstep %}

{% step %}

### Insert the merge field in Word

Place your cursor where the value should appear.

Then insert or type the DOCX merge field syntax:

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

{% endstep %}

{% step %}

### Add formatting if needed

Formatting stays inside the merge field.

Example:

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

{% endstep %}

{% step %}

### Upload and test

Upload the `.docx` file to the template.

Generate a document from a real record.

Confirm the value and formatting are correct.
{% endstep %}
{% endstepper %}

### Quick examples

#### Plain text field

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

#### Parent relationship field

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

#### Number formatting

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

#### Date formatting

```plaintext
[{{!Opportunity.CloseDate MM/dd/yyyy}}]
```

#### Merge field inside a conditional block

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

### Quick reference: common DOCX merge field patterns

This is a practical reference for the patterns you will use most.

#### Standard field output

Use square brackets around the field.

```plaintext
[{{!Object.Field}}]
[{{!Opportunity.Account.Name}}]
```

#### Number formatting

Apply the format inside the field.

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

#### Date formatting

Apply the date pattern inside the field.

```plaintext
[{{!Opportunity.CloseDate MM/dd/yyyy}}]
[{{!Opportunity.CreatedDate yyyy-MM-dd}}]
```

#### Replace characters or values

Use `replaceAll` inside the field.

```plaintext
[{{!Contact.LastName replaceAll=\"',\\\\'\"}}]
```

Use `#comma#` if a replacement value must contain a comma.

#### Named query output

Wrap the output field in square brackets.

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

#### Conditional output

Wrap both `RENDER` tags in square brackets.

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

### Common issues

* Merge field prints as text because the square brackets are missing
* Field path is wrong or incomplete
* `replaceAll` syntax is malformed
* Query output field is not wrapped in square brackets
* Word proofreading markup breaks generation

{% hint style="warning" %}
If a DOCX file fails to generate, clear Word spelling and grammar marks before upload.
{% endhint %}

### Related articles

* [Microsoft Word (DOCX)](/sdocs/template-architecture/document-formats/docx-s-docs.md)
* [How to: Create a Table In Microsoft Templates (DOCX, PPTX, XLSX)](/sdocs/advanced-template-logic/related-lists/how-to-create-a-table-in-microsoft-templates-docx-pptx.md)
* [DOCX Generation Failure: SpellStart](/sdocs/template-architecture/document-formats/docx-s-docs/additional-resources/docx-generation-failure-spellstart.md)
* [Escape an Apostrophe Correctly in a Merge Field](/sdocs/template-architecture/inserting-merge-fields/additional-resources/escape-an-apostrophe-correctly-in-a-merge-field.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/inserting-merge-fields/merge-fields-with-microsoft-templates-docx-pptx-xlsx.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.
