Extract Comments from Word Document and Save in TXT File in C#, VB.NET

Word comments can be reviews or thoughts about part of contents or explanations or references of specified phrase, sentence or paragraph given by author. Also, the existing comments can be extracted from document and solution in this guide demonstrates how to extract Word comments and save to TXT file in C# and Visual Basic via Spire.Doc for .NET.

Spire.Doc for .NET, one easy-to-use .NET Word component to preform Word tasks, provides a Comment class to enable users to get comments in Word and paragraphs of comment body. The screenshot below shows the original documents with two comments.

Extract Word Commnet

Download and install Spire.Doc for .NET and follow the steps to extract Word comments. Firstly, initialize a StringBuilder instance to save extracted comments. Secondly, use a foreach statement to get all comments in Word and use another foreach statement to get each paragraph of body of each comment. Then, invoke StringBuilder.AppendLine method to append a copy of comment string followed by default line terminator to the end of the current StringBuilder object. The parameter passed to this method is string value which is comment paragraph text. Thirdly, invoke File.WrtieAllText method to create a new TXT file with comments text as contents. The parameters passed to this method are string path and string contents. Code as following:

[C#]
using System.Text;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace ExtractComments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Document
            Document doc = new Document();
            doc.LoadFromFile(@"E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx");

            //Extract Comment
            StringBuilder SB = new StringBuilder();

            foreach(Comment comment in doc.Comments)
            {
                foreach (Paragraph p in comment.Body.Paragraphs)
                {
                    SB.AppendLine(p.Text);
                }
            }

            //Save to TXT File
            File.WriteAllText("CommentExtraction.txt", SB.ToString());
            System.Diagnostics.Process.Start("CommentExtraction.txt");
        }
    }
}

 

[Visual Basic]
Imports System.Text
Imports System.IO
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace ExtractComments
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim doc As New Document()
            doc.LoadFromFile("E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx")

            'Extract Comment
            Dim SB As New StringBuilder()

            For Each comment As Comment In doc.Comments
                For Each p As Paragraph In comment.Body.Paragraphs
                    SB.AppendLine(p.Text)
                Next p
            Next comment

            'Save to TXT File
            File.WriteAllText("CommentExtraction.txt", SB.ToString())
            System.Diagnostics.Process.Start("CommentExtraction.txt")
        End Sub
    End Class
End Namespace

After debugging, the following result will be presented:

Spire.Doc, a professional Word component, enables developers/programmers to operate Word document, for example, generating, opening, saving and modifying on .NET, WPF and Silverlight applications