Iterating Over Word Documents in C# and VB.NET

GemBox.Document represents a Word document with an in-memory content model that has a tree‑like structure and a DocumentModel as a root element.

You can traverse through the document elements by following their parent/child relationships.

Alternatively, you can use one of the Element.GetChildElements methods, which provide a way to flatten the document representation so it can be iterated.

The following example shows how you can iterate over a document’s content and filter specific types of elements.

Iterating through Word file's content model
Screenshot of Word document's iteration
Upload your file (Drag file here)
using System;
using System.Linq;
using GemBox.Document;

class Program
    static void Main()
        // If using Professional version, put your serial key below.

        var document = DocumentModel.Load("%InputFileName%");

        int numberOfSections = document.Sections.Count;
        int numberOfParagraphs = document.GetChildElements(true, ElementType.Paragraph).Count();
        int numberOfRunsAndFields = document.GetChildElements(true, ElementType.Run, ElementType.Field).Count();

        var section = document.Sections[0];

        int numberOfElements = section.GetChildElements(true).Count();
        int numberOfBlocks = section.GetChildElements(true).OfType<Block>().Count();
        int numberOfInlines = section.GetChildElements(true).OfType<Inline>().Count();

        Console.WriteLine("File has:");
        Console.WriteLine($" - {numberOfSections} sections.");
        Console.WriteLine($" - {numberOfParagraphs} paragraphs.");
        Console.WriteLine($" - {numberOfRunsAndFields} runs and fields.");


        Console.WriteLine("First section has:");
        Console.WriteLine($" - {numberOfElements} elements.");
        Console.WriteLine($" - {numberOfBlocks} blocks.");
        Console.WriteLine($" - {numberOfInlines} inlines.");
Imports System
Imports System.Linq
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim document As DocumentModel = DocumentModel.Load("%InputFileName%")

        Dim numberOfSections As Integer = document.Sections.Count
        Dim numberOfParagraphs As Integer = document.GetChildElements(True, ElementType.Paragraph).Count()
        Dim numberOfRunsAndFields As Integer = document.GetChildElements(True, ElementType.Run, ElementType.Field).Count()

        Dim section = document.Sections(0)

        Dim numberOfElements As Integer = section.GetChildElements(True).Count()
        Dim numberOfBlocks As Integer = section.GetChildElements(True).OfType(Of Block)().Count()
        Dim numberOfInlines As Integer = section.GetChildElements(True).OfType(Of Inline)().Count()

        Console.WriteLine("File has:")
        Console.WriteLine($" - {numberOfSections} sections.")
        Console.WriteLine($" - {numberOfParagraphs} paragraphs.")
        Console.WriteLine($" - {numberOfRunsAndFields} runs and fields.")


        Console.WriteLine("First section has:")
        Console.WriteLine($" - {numberOfElements} elements.")
        Console.WriteLine($" - {numberOfBlocks} blocks.")
        Console.WriteLine($" - {numberOfInlines} inlines.")

    End Sub
End Module

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.