Microsoft Word provides you the ability to add comments to Word documents. Comments can be helpful in cases such as suggesting improvements in documents or sharing thoughts on the text. There might be situations where you need to manage comments programmatically. To that end, this article will teach you how to work with comments in Word documents using C++.
- C++ API for Working with Comments in Word Documents
- Add Comments to Word Documents using C++
- Read Comments from a Word Document
- Modify Comments in a Word Document using C++
- Delete Comments from a Word Document using C++
C++ API for Working with Comments in Word Documents
Aspose.Words for C++ is a native C++ library that allows you to create, read, modify and convert Microsoft Word documents. In addition, it also supports working with comments in DOCX and DOC files. You can either install the API through NuGet or download it directly from the Downloads section.
PM> Install-Package Aspose.Words.Cpp
Add Comments to Word Documents using C++
Aspose.Words for C++ API provides the ability to add comments with the author name, initials, and date/time. The following are the steps to add comments to Word documents.
- Firstly, load the Word document using the Document class.
- Create an instance of the DocumentBuilder class by passing the Document object created in the previous step.
- Create the comment using the Comment class.
- Add the comment to the document using the DocumentBuilder->get_CurrentParagraph()->AppendChild(System::SharedPtrAspose::Words::Node newChild) method.
- Finally, save the document using the Document->Save(System::String fileName) method.
The following sample code demonstrates how to add comments to Word documents using C++.
The following is the image of the output generated by the sample code.
Read Comments from a Word Document
The following are the steps to read comments from Word documents.
- Load the Word document using the Document class.
- Retrieve the comments using the Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep) method.
- Iterate through the comments and retrieve the information of individual comments.
The following is the sample code to read comments from a Word document using C++.
Modify Comments in a Word Document using C++
In order to modify a comment, retrieve it using the NodeCollection->idx_get(int32_t index) method and change it according to your needs. The following are the steps to modify comments in a Word document.
- Firstly, load the Word document using the Document class.
- Retrieve the comments using the Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep) method.
- Get the desired comment using the NodeCollection->idx_get(int32_t index) method and casting the result to Comment.
- Update the comment.
- Finally, save the document using the Document->Save(System::String fileName) method.
The following sample code shows how to modify comments in a Word document using C++.
Delete Comments from a Word Document using C++
The Aspose.Words for C++ API provides multiple ways to delete comments from Word documents. In this section, you will learn how to delete a specific comment, comments by author, and all comments using C++.
Delete a Specific Comment
The following are the steps to delete a specific comment.
- Load the Word document using the Document class.
- Retrieve the comments using the Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep) method.
- Get the comment you want to delete using the NodeCollection->idx_get(int32_t index) method and casting the result to Comment.
- Delete the comment using the Comment->Remove() method.
- Save the document using the Document->Save(System::String fileName) method.
The following sample code shows how to delete a specific comment from a Word document using C++.
Deleting Comments by Author
The following are the steps to delete comments by author.
- Firstly, load the Word document using the Document class.
- Retrieve the comments using the Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep) method.
- Loop through the comments.
- Within the loop, retrieve the comment and compare its author. If the author matches, delete the comment.
- Finally, save the document using the Document->Save(System::String fileName) method.
The following is the sample code to delete comments by author using C++.
Delete All Comments
Instead of deleting individual comments, you can delete all the comments at once using the NodeCollection->Clear() method. The following are the steps to delete all comments from a Word document.
- Load the Word document using the Document class.
- Retrieve the comments using the Document->GetChildNodes(Aspose::Words::NodeType nodeType, bool isDeep) method.
- Remove all the comments using the NodeCollection->Clear() method.
- Finally, save the document using the Document->Save(System::String fileName) method.
The following sample code demonstrates how to delete all the comments from a Word document using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to work with comments in Word documents using C++. Specifically, you have learned how to add, read, edit and delete comments. Furthermore, you have seen how to delete a specific comment, comments by author, and all comments using Aspose.Words for C++ API. The API provides a vast number of additional features for automating your Word-related tasks. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.