I'm trying to loop through all comments in a Word Doc and Clear all the comments that contain a substring of "CME" in the comment text. The application is a VSTO Add-In written in C# and it's using .NET 4.8.
I have 2 problems.
- The delete method isn't in the list of methods I can choose here Globals.ThisAddIn.Application.ActiveDocument.Comments[i]
- When I use the DeleteRecursively() Method, it doesn't delete all the comments, it leaves one and I have to click the clear comments button on the ribbon again to get rid of the last one.
What I have tried.
- I have installed office interop library via nuget.
- I have tried looking for other methods on the Comment in the collection, but haven't found anything that works.
- Searched the web.
Here is the method
private void btnClearComments_Click(object sender, RibbonControlEventArgs e)
{
if (Globals.ThisAddIn.Application.ActiveDocument.Comments.Count != 0)
{
// Globals.ThisAddIn.Application.ActiveDocument.DeleteAllComments();
// MessageBox.Show("Clearing All Comments ");
for(int i = 1; i <= Globals.ThisAddIn.Application.ActiveDocument.Comments.Count; i++)
{
if (Globals.ThisAddIn.Application.ActiveDocument.Comments[i].Range.Text.Contains("CME"))
{
Globals.ThisAddIn.Application.ActiveDocument.Comments[i].DeleteRecursively();
}
}
}
else
{
MessageBox.Show("There are No Comments to Delete");
}
}
Any Help would be appreciated, thanks.