Setting Comment.Reference.Font.Hidden doesn't work

102 Views Asked by At

I want to programmatically hide specific comments in a Word document. I'm trying to do that by setting the Hidden property of the comment's reference. However, when I do so, it does not change the visibility of the comment.

I can hide the content of the comment by doing the same thing to the comment's range, but this leaves the balloon and line pointing to the text in the document body.

I see that I can hide a comment manually in the UI by selecting it and setting the font's hidden property. Therefore I think I should be able to achieve the same thing programmatically.

I could use a workaround of setting the author to 'Not displayed' and filtering the view to exclude revisions by 'Not displayed', then putting the author back to its original value when it is to be displayed again.

I would prefer to just be able to hide the comment directly without this change, so I would still be interested in a way of doing that.

1

There are 1 best solutions below

0
joeschwa On

Using Word 2019, C#, and VSTO I was able to hide all comments in the ActiveDocument with

Word.Range myRange = Globals.ThisAddIn.Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdCommentsStory];
myRange.Font.Hidden = 1; //hides all comments in document

myRange.Font.Hidden = 0; displays all hidden comments.

However, hiding specific comments with their author names requires first hiding all comments and then displaying the comment(s) that one wants to be viewable. In the below example I am hiding all comments except the second one in the collection.

Word.Range myRange = Globals.ThisAddIn.Application.ActiveDocument.StoryRanges[Word.WdStoryType.wdCommentsStory];
myRange.Font.Hidden = 1; //hides all comments in document

foreach (Word.Comment myComment in myRange.Comments)
{
     if (myComment.Index == 2)
         myComment.Range.Font.Hidden = 0;
}

If all comments are not hidden before restoring the ones that need to be visible, the comment text of the comment is hidden, but the author name remains visble.