Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Working With Runs

Last modified: January 13, 2011

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

In this article
Runs in WordprocessingML
Run Class
Example

This topic discusses the Open XML SDK 2.0Run class and how it relates to the Open XML File Format WordprocessingML schema.

The following text from the ISO/IEC 29500 specification introduces the Open XML WordprocessingML run element.

The next level of the document hierarchy [after the paragraph] is the run, which defines a region of text with a common set of properties. A run is represented by an r element, which allows the producer to combine breaks, styles, or formatting properties, applying the same information to all the parts of the run.

Just as a paragraph can have properties, so too can a run. All of the elements inside an r element have their properties controlled by a corresponding optional rPr run properties element, which must be the first child of the r element. In turn, the rPr element is a container for a set of property elements that are applied to the rest of the children of the r element. The elements inside the rPr container element allow the consumer to control whether the text in the following t elements is bold, underlined, or visible, for example. Some examples of run properties are bold, border, character style, color, font, font size, italic, kerning, disable spelling/grammar check, shading, small caps, strikethrough, text direction, and underline.

© ISO/IEC29500: 2008.

The following table lists the most common Open XML SDK classes used when working with runs.

XML element

Open XML SDK 2.0 Class

p

Paragraph

rPr

RunProperties

t

Text

The OXML SDK Run class represents the run (<r>) element defined in the Open XML File Format schema for WordprocessingML documents as discussed above. Use a Run object to manipulate an individual <r> element in a WordprocessingML document.

RunProperties Class

In WordprocessingML, the properties for a run element are specified using the run properties (<rPr>) element. Some examples of run properties are bold, border, character style, color, font, font size, italic, kerning, disable spelling/grammar check, shading, small caps, strikethrough, text direction, and underline. Use a RunProperties object to set the properties for a run in a WordprocessingML document.

Text Object

With the <r> element, the text (<t>) element is the container for the text that makes up the document content. The OXML SDK Text class represents the <t> element. Use a Text object to place text in a Wordprocessing document.

The following code adds text to the main document surface of the specified WordprocessingML document. A Run object demarcates a region of text within the paragraph and then a RunProperties object is used to apply bold formatting to the run.

public static void WriteToWordDoc(string filepath, string txt)
{
    // Open a WordprocessingDocument for editing using the filepath.
    using (WordprocessingDocument wordprocessingDocument =
         WordprocessingDocument.Open(filepath, true))
    {
        // Assign a reference to the existing document body.
        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        // Add new text.
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());

        // Apply bold formatting to the run.
        RunProperties runProperties = run.AppendChild(new RunProperties(new Bold()));   
        run.AppendChild(new Text(txt));                
    }
}

When this code is run, the following XML is written to the WordprocessingML document specified in the preceding code.

<w:p>
  <w:r>
    <w:rPr>
      <w:b />
    </w:rPr>
    <w:t>String from WriteToWordDoc method.</w:t>
  </w:r>
</w:p>

Show:
© 2015 Microsoft