In collaborative editing and review processes, comments in Microsoft Word documents play an important role. Whether you're managing a team project, conducting document reviews, or analyzing feedback, extracting comments programmatically can save time and increase productivity.

Comments in all supported document formats, including Office Open XML, are supported through a comprehensive interface in TX Text Control. It allows users to add and edit comments using a full-featured interface, including inline editing and sidebars. This article will guide you through the process of extracting comments from MS Word documents using C#, allowing you to streamline workflows and integrate comments into your applications.

Why Extract Comments Programmatically?

There are several benefits to automating the extraction of comments from Word documents:

  • Efficiency: Process large volumes of documents quickly without manual intervention.
  • Integration: Import comments into project management tools or databases for further analysis.
  • Analysis: In collaborative projects, identify common feedback trends or issues.
  • Automation: Streamline workflows and reduce manual tasks.

Creating review reports, tracking feedback for compliance, and analyzing document collaboration patterns are common use cases.

Creating the Application

To demonstrate how easy this is with the TX Text Control library, we will use a .NET console application.

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 8 SDK.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server for ASP.NET.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select Console App as the project template and confirm with Next.

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 8 (Long-term support) as the Framework and confirm with Create.

Adding the NuGet Package

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    ASP.NET Core Web Application

Extracting Comments

For this tutorial, we will use a sample document that contains comments from two different authors.

MS Word DOCX Document with Comments

To extract the commented text and the comment itself, the following code in Program.cs will iterate through all comments.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
// Create a new instance of ServerTextControl
tx.Create();
// Load the document "Lorem Ipsum.docx" in WordprocessingML format
tx.Load("Lorem Ipsum.docx", TXTextControl.StreamType.WordprocessingML);
// Iterate through each commented text in the document
foreach (TXTextControl.CommentedText commentedText in tx.Comments)
{
// Output the commented text and its associated comment to the console
Console.WriteLine($"Commented Text: {commentedText.Text}, Comment: {commentedText.Comment}");
}
}
view raw test.cs hosted with ❤ by GitHub
Commented Text: amet, Comment: That may be a very good point.
Commented Text: amet, Comment: Very helpful!
Commented Text: Nunc, Comment: We probably need to explain this in more detail.
Commented Text: magna, Comment: This is not necessary IMHO

Filter Comments

Filtering comments by various properties such as author, date, or text is a common requirement. The following code snippet demonstrates how to filter comments by author name:

using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
// Initialize a new instance of ServerTextControl
tx.Create();
// Load the document "Lorem Ipsum.docx" in WordprocessingML format
tx.Load("Lorem Ipsum.docx", StreamType.WordprocessingML);
// Flatten comments into a single list
var flatComments = FlattenComments(tx.Comments.Cast<CommentedText>());
// Filter comments by user email and print their text
flatComments.Where(comment => comment.UserName == "account@textcontrol.com")
.ToList()
.ForEach(comment => Console.WriteLine(comment.Text));
}
/// <summary>
/// Recursively flattens a list of comments and their replies into a single list.
/// </summary>
/// <param name="comments">The collection of comments to flatten.</param>
/// <returns>A flattened list of comments.</returns>
static List<CommentedText> FlattenComments(IEnumerable<CommentedText> comments)
{
var flatList = new List<CommentedText>();
foreach (CommentedText comment in comments)
{
flatList.Add(comment); // Add the current comment
if (comment.Replies != null && comment.Replies.Any())
{
// Recursively add replies
flatList.AddRange(FlattenComments(comment.Replies));
}
}
return flatList;
}
view raw test.cs hosted with ❤ by GitHub

First, the entire collection of CommentedText TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
CommentedText Class
A CommentedText object represents a commented piece of text.
objects is flattened because each comment can recursively contain replies.

That may be a very good point.
Very helpful!
Very helpful!
We probably need to explain this in more detail.
This is not necessary IMHO

The following modified lambda expression filters comments by author name and creation timestamp:

// Filter comments by username and date later than 2025-01-15
flatComments
.Where(comment => comment.UserName == "account@textcontrol.com"
&& comment.CreationTime > new DateTime(2025, 1, 15))
.ToList()
.ForEach(comment => Console.WriteLine($"{comment.UserName} - {comment.CreationTime} - {comment.Text}"));
view raw test.cs hosted with ❤ by GitHub
account@textcontrol.com - 1/17/2025 10:34:00 AM - amet
account@textcontrol.com - 1/17/2025 10:35:00 AM - amet
account@textcontrol.com - 1/17/2025 10:35:00 AM - amet
account@textcontrol.com - 1/17/2025 10:35:00 AM - Nunc
account@textcontrol.com - 1/17/2025 10:35:00 AM - magna

Conclusion

Programmatically extracting comments from Word documents can save time and increase productivity in collaborative projects. By automating the extraction process, you can integrate comments into your applications, analyze feedback trends, and streamline workflows. The TX Text Control library provides a comprehensive interface for working with comments in all supported document formats, allowing you to easily extract comments programmatically.