# Named Queries In Microsoft Templates (DOCX, PPTX, XLSX)

Use this page when your named query works in the HTML editor but needs DOCX or PPTX syntax.

The query logic stays the same.

The wrapping syntax changes.

### Core rule

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

### Basic pattern

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

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

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

{% endcode %}

Use the same query name and field names you would use in HTML templates.

The only difference is the surrounding square brackets.

### Use `filter` and `offset`

`filter` and `offset` work in DOCX and PPTX too.

{% code title="docx-pptx-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 %}

### Common fixes

* **Blank output:** Confirm the field appears in `SELECT`
* **Wrong row:** Check `ORDER BY` before using `offset`
* **Filter fails:** Match `filter="1"` to the `<filter id>` value exactly
* **No reusable output:** Keep `<class>none</class>` in the query block
* **Syntax issue in DOCX or PPTX:** Make sure both the query block and the merge field use square brackets

{% hint style="info" %}
Build and test the named query in the HTML editor first.

Then switch to DOCX or PPTX syntax.
{% endhint %}

### Keep going

* Review [Named Query Structure](/developer-hub/template-configuration-and-styling/reference-named-query-syntax/named-query-structure.md) for the base tag and merge field rules
* Review [Named Query Working Examples](/developer-hub/template-configuration-and-styling/reference-named-query-syntax/named-query-working-examples.md) for more reusable query patterns
* Review [How to: Advanced Data Retrieval with Named Queries](https://github.com/kvantiem-sdocs/Developer-Hub-Documentation/blob/main/advanced-template-logic/named-query/how-to-advanced-data-retrieval-with-named-queries.md) for aggregates, filters, and offsets


---

# 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/developer-hub/template-configuration-and-styling/reference-named-query-syntax/named-queries-in-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.
