Skip to main content
All docs
V24.2

STYLEREF

  • 3 minutes to read

Non-MailMerge Field

{ STYLEREF “styleName” [switches] }

Inserts text that is formatted with the specified style.

The Word Processing Document API searches the document for text formatted with the specified style. The first found text is the STYLEREF field result.

The search algorithm depends on the document part where the STYLEREF field is located.

Main Document Body
The Word Processing Document API searches the document from the current paragraph to the start of the document. If the formatted text is not found, the Word Processing Document API then searches from the current paragraph to the end of the document.
Text Boxes, Comments, Footnotes, and Endnotes
The same search algorithm as in the main document body. The search starts from the paragraph that contains the text box’s anchor, footnote position, or endnote position. If the field is in a comment, the starting paragraph is the document range to which the comment is applied.
Headers and Footers
The Word Processing Document API searches the current page from top to bottom. If the formatted text is not found, the Word Processing Document API then searches from the top of the current page to the start of the document, and then from the bottom of the page to the end of the document.

The STYLEREF field allows you to insert text formatted with a paragraph or character style. Use the Document.ParagraphStyles and Document.CharacterStyles properties to access the collection of paragraph and character styles.

The STYLEREF field supports the following switches:

Switch Description
\l Inserts the last text formatted with the specified style. Changes the search direction on the current page (from bottom to top instead of top to bottom). This switch is available if the STYLEREF field is in the header or footer.
\n Inserts the paragraph number of the referenced text. Does not include previous level numbers.
\p Inserts the field position relative to the paragraph with the referenced text. If the STYLEREF field is in the document before this paragraph, it inserts the text “below”. If the STYLEREF field is after this paragraph, it inserts the text “above”. If the STYLEREF field is in the paragraph, an error is returned. When used with \n, \r, \s, or \w switches, appends the text “above” or “below” to the end of the paragraph number. This switch is not available if the STYLEREF field is in the header or footer.
\r Inserts the paragraph number of the referenced text relative to the context or paragraph numbering scheme. If the field and the referenced text are on different paragraph levels, the field result includes all level numbers between them.
\t When used with \n, \r, and \w switches, deletes all non-limiter and non-numerical characters from the paragraph number.
\w Inserts the entire paragraph number of the referenced text from any part of the document.
\s Inserts the entire paragraph number of the referenced text, excluding all non-limiter and non-numerical characters.