.NET Word Comment-Remove Word Comments in C# and Visual Basic

Word Comments, are not only used to represent readers’ opinions or reviews in contents of the document, but also be explanation of some phrases or sentences provided by author. If the contents with comments are modified or deleted, comments will be removed automatically. Or, you can select the specified comment and delete it manually if it is unnecessary any more. Now, this post will introduce an easy solution to remove Word comments in C# and Visual.

This solution is based on a .NET Word Library, Spire.Doc for .NET. So I add its dll file as reference after installing. Besides, the other thing we should prepare is a Word document with comments. The following screenshot presents contents of this document.

Firstly, we should load the document with comments by invoking LoadFrom method of Document class.

Secondly, because comments are stored in Document, so we need to get CommentCollection from Document at first, then invoke CommentCollection.RemoveAt(int index) method to remove specified comments.

Thirdly, invoke SaveToFile method of Document class to save new document without comments.

C#

using Spire.Doc;

namespace RemoveComment

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load Document

            Document document = new Document();

            document.LoadFromFile(@”E:\work\Documents\WordDocuments\New Zealand.docx”);

 

            //Remove Comment

            document.Comments.RemoveAt(0);

 

            //Save and Launch

            document.SaveToFile(“RemoveComment.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“RemoveComment.docx”);

        }

    }

}

Visual Basic

Imports Spire.Doc

 

Namespace RemoveComment

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load Document

            Dim document As New Document()

            document.LoadFromFile(“E:\work\Documents\WordDocuments\New Zealand.docx”)

 

            ‘Remove Comment

            document.Comments.RemoveAt(0)

 

            ‘Save and Launch

            document.SaveToFile(“RemoveComment.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“RemoveComment.docx”)

        End Sub

    End Class

End Namespace

After debugging, we can find that the first comment have been removed.

Download Spire.Doc for .NET Here

P.S

The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

1 thought on “.NET Word Comment-Remove Word Comments in C# and Visual Basic”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s