Поделиться через


How to: Programmatically display worksheet comments

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can programmatically show and hide comments in Microsoft Office Excel worksheets.

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.

To display all comments on a worksheet in a document-level customization

  1. Set the Visible property to true if you want to show comments; otherwise false. This code must be placed in a sheet class, not in the ThisWorkbook class.

    private void ShowOrHideComments(bool show)
    {
        for (int i = 1; i <= this.Comments.Count; i++)
        {
            this.Comments[i].Visible = show;
        }
    }
    
    Private Sub ShowOrHideComments(ByVal show As Boolean)
        Dim i As Integer
        For i = 1 To Me.Comments.Count
            Me.Comments(i).Visible = show
        Next
    End Sub
    

To display all comments on a worksheet in an application-level VSTO Add-in

  1. Set the Visible property to true if you want to show comments; otherwise false.

    private void ShowOrHideComments(bool show)
    {
        Excel.Worksheet worksheet = (Excel.Worksheet)Application.ActiveSheet;
        for (int i = 1; i <= worksheet.Comments.Count; i++)
        {
            worksheet.Comments[i].Visible = show;
        }
    }
    
    Private Sub ShowOrHideComments(ByVal show As Boolean)
        Dim worksheet As Excel.Worksheet = CType(Application.ActiveSheet, Excel.Worksheet)
        Dim i As Integer
        For i = 1 To worksheet.Comments.Count
            worksheet.Comments(i).Visible = show
        Next
    End Sub
    

See also