GlossaryDocument class

Represents the root element for a glossary document within a Word document. A glossary document is a storage for AutoText, AutoCorrect entries and Building Blocks.

public class GlossaryDocument : DocumentBase


Name Description
GlossaryDocument() The default constructor.


Name Description
BackgroundShape { get; set; } Gets or sets the background shape of the document. Can be null.
BuildingBlocks { get; } Returns a typed collection that represents all building blocks in the glossary document.
ChildNodes { get; } Gets all immediate child nodes of this node.
Count { get; } Gets the number of immediate children of this node.
CustomNodeId { get; set; } Specifies custom node identifier.
override Document { get; }
FirstBuildingBlock { get; } Gets the first building block in the glossary document.
FirstChild { get; } Gets the first child of the node.
FontInfos { get; } Provides access to properties of fonts used in this document.
HasChildNodes { get; } Returns true if this node has any child nodes.
override IsComposite { get; } Returns true as this node can have child nodes.
LastBuildingBlock { get; } Gets the last building block in the glossary document.
LastChild { get; } Gets the last child of the node.
Lists { get; } Provides access to the list formatting used in the document.
NextSibling { get; } Gets the node immediately following this node.
NodeChangingCallback { get; set; } Called when a node is inserted or removed in the document.
override NodeType { get; } Returns the GlossaryDocument value.
PageColor { get; set; } Gets or sets the page color of the document. This property is a simpler version of BackgroundShape.
ParentNode { get; } Gets the immediate parent of this node.
PreviousSibling { get; } Gets the node immediately preceding this node.
Range { get; } Returns a Range object that represents the portion of a document that is contained in this node.
ResourceLoadingCallback { get; set; } Allows to control how external resources are loaded.
Styles { get; } Returns a collection of styles defined in the document.
WarningCallback { get; set; } Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss.


Name Description
override Accept(DocumentVisitor) Accepts a visitor.
AppendChild(Node) Adds the specified node to the end of the list of child nodes for this node.
Clone(bool) Creates a duplicate of the node.
CreateNavigator() Reserved for system use. IXPathNavigable.
GetAncestor(NodeType) Gets the first ancestor of the specified NodeType.
GetAncestor(Type) Gets the first ancestor of the specified object type.
GetBuildingBlock(BuildingBlockGallery, string, string) Finds a building block using the specified gallery, category and name.
GetChild(NodeType, int, bool) Returns an Nth child node that matches the specified type.
GetChildNodes(NodeType, bool) Returns a live collection of child nodes that match the specified type.
GetEnumerator() Provides support for the for each style iteration over the child nodes of this node.
override GetText() Gets the text of this node and of all its children.
ImportNode(Node, bool) Imports a node from another document to the current document.
ImportNode(Node, bool, ImportFormatMode) Imports a node from another document to the current document with an option to control formatting.
IndexOf(Node) Returns the index of the specified child node in the child node array.
InsertAfter(Node, Node) Inserts the specified node immediately after the specified reference node.
InsertBefore(Node, Node) Inserts the specified node immediately before the specified reference node.
NextPreOrder(Node) Gets next node according to the pre-order tree traversal algorithm.
PrependChild(Node) Adds the specified node to the beginning of the list of child nodes for this node.
PreviousPreOrder(Node) Gets the previous node according to the pre-order tree traversal algorithm.
Remove() Removes itself from the parent.
RemoveAllChildren() Removes all the child nodes of the current node.
RemoveChild(Node) Removes the specified child node.
RemoveSmartTags() Removes all SmartTag descendant nodes of the current node.
SelectNodes(string) Selects a list of nodes matching the XPath expression.
SelectSingleNode(string) Selects the first Node that matches the XPath expression.
ToString(SaveFormat) Exports the content of the node into a string in the specified format.
ToString(SaveOptions) Exports the content of the node into a string using the specified save options.


Some documents, usually templates, can contain AutoText, AutoCorrect entries and/or Building Blocks (also known as glossary document entries, document parts or building blocks).

To access building blocks, you need to load a document into a Document object. Building blocks will be available via the GlossaryDocument property.

GlossaryDocument can contain any number of BuildingBlock objects. Each BuildingBlock represents one document part.

Corresponds to the glossaryDocument and docParts elements in OOXML.


Shows ways of accessing building blocks in a glossary document.

public void GlossaryDocument()
    Document doc = new Document();
    GlossaryDocument glossaryDoc = new GlossaryDocument();

    glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 1" });
    glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 2" });
    glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 3" });
    glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 4" });
    glossaryDoc.AppendChild(new BuildingBlock(glossaryDoc) { Name = "Block 5" });

    Assert.AreEqual(5, glossaryDoc.BuildingBlocks.Count);

    doc.GlossaryDocument = glossaryDoc;

    // There are various ways of accessing building blocks.
    // 1 -  Get the first/last building blocks in the collection:
    Assert.AreEqual("Block 1", glossaryDoc.FirstBuildingBlock.Name);
    Assert.AreEqual("Block 5", glossaryDoc.LastBuildingBlock.Name);

    // 2 -  Get a building block by index:
    Assert.AreEqual("Block 2", glossaryDoc.BuildingBlocks[1].Name);
    Assert.AreEqual("Block 3", glossaryDoc.BuildingBlocks.ToArray()[2].Name);

    // 3 -  Get the first building block that matches a gallery, name and category:
    Assert.AreEqual("Block 4", 
        glossaryDoc.GetBuildingBlock(BuildingBlockGallery.All, "(Empty Category)", "Block 4").Name);

    // We will do that using a custom visitor,
    // which will give every BuildingBlock in the GlossaryDocument a unique GUID
    GlossaryDocVisitor visitor = new GlossaryDocVisitor();


    // In Microsoft Word, we can access the building blocks via "Insert" -> "Quick Parts" -> "Building Blocks Organizer".
    doc.Save(ArtifactsDir + "BuildingBlocks.GlossaryDocument.dotx"); 

/// <summary>
/// Gives each building block in a visited glossary document a unique GUID.
/// Stores the GUID-building block pairs in a dictionary.
/// </summary>
public class GlossaryDocVisitor : DocumentVisitor
    public GlossaryDocVisitor()
        mBlocksByGuid = new Dictionary<Guid, BuildingBlock>();
        mBuilder = new StringBuilder();

    public string GetText()
        return mBuilder.ToString();

    public Dictionary<Guid, BuildingBlock> GetDictionary()
        return mBlocksByGuid;

    public override VisitorAction VisitGlossaryDocumentStart(GlossaryDocument glossary)
        mBuilder.AppendLine("Glossary document found!");
        return VisitorAction.Continue;

    public override VisitorAction VisitGlossaryDocumentEnd(GlossaryDocument glossary)
        mBuilder.AppendLine("Reached end of glossary!");
        mBuilder.AppendLine("BuildingBlocks found: " + mBlocksByGuid.Count);
        return VisitorAction.Continue;

    public override VisitorAction VisitBuildingBlockStart(BuildingBlock block)
        block.Guid = Guid.NewGuid();
        mBlocksByGuid.Add(block.Guid, block);
        return VisitorAction.Continue;

    public override VisitorAction VisitBuildingBlockEnd(BuildingBlock block)
        mBuilder.AppendLine("\tVisited block \"" + block.Name + "\"");
        mBuilder.AppendLine("\t Type: " + block.Type);
        mBuilder.AppendLine("\t Gallery: " + block.Gallery);
        mBuilder.AppendLine("\t Behavior: " + block.Behavior);
        mBuilder.AppendLine("\t Description: " + block.Description);

        return VisitorAction.Continue;

    private readonly Dictionary<Guid, BuildingBlock> mBlocksByGuid;
    private readonly StringBuilder mBuilder;

See Also