Speak

Speech is fun to demo, but it’s not really a mainstream feature. Most often it is used to enable people with disabilities to read spreadsheets, but you can use it to read any text. For example, the following code reads comments aloud whenever a cell with a comment receives focus:

' ThisWorkbook object
Dim WithEvents g_app As Application
 
' Hook up the global Application object handler when this
' workbook opens.
Private Sub Workbook_Open(  )
    If Not (g_app Is Nothing) Then _
      Set g_app = Application
End Sub
 
' Read comments aloud when cell is selected.
Private Sub g_app_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Range)
    ' If the cell has a comment.
    If Not (Target.Comment Is Nothing) Then
        On Error Resume Next
        ' Read the comment text aloud.
        Application.Speech.Speak Target.Comment.Text
    End If
End Sub
 
' Unhook handler
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Set g_app = Nothing
End Sub

It is important to turn on error handling in case speech is not installed on the user’s system.

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.