玩在Excel中使用VBA声音笔记
在Excel 5和95中,可以使用声音文件将注释附加到单元格上。通过打开用于编辑单元格注释的对话框可以播放此声音注释。
下面的宏还可以为您播放附加到单元格上的音符:
Sub PlaySoundNotesInExcel95(CellAddress As String) ' for Excel 5 and 95 only If Not Application.CanPlaySounds Then Exit Sub On Error Resume Next ' in case there is no soundnote Range(CellAddress).SoundNote.Play On Error GoTo 0 End Sub
Excel 97或更高版本不再支持使用音符。
通过下面的宏,可以创建一种变通办法以实现相同的效果:
Public Declare Function sndPlaySound Lib "winmm.dll" _ Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _ ByVal uFlags As Long) As Long Sub PlayWavFile(WavFileName As String, Wait As Boolean) If Dir(WavFileName) = "" Then Exit Sub ' no file to play If Wait Then ' play sound before running any more code sndPlaySound WavFileName, 0 Else ' play sound while code is running sndPlaySound WavFileName, 1 End If End Sub Sub PlaySoundNotesInExcel97(CellAddress As String) ' workaround for playing sound notes in Excel 97 or later Dim SoundFileName As String SoundFileName = "" On Error Resume Next ' an error occurs if the cell doesn't have a note SoundFileName = Range(CellAddress).Comment.Text On Error GoTo 0 If SoundFileName = "" Then Exit Sub ' no cell note If InStr(1, SoundFileName, Chr(10)) > 0 Then ' the note contains a line-break ' use the first line as the filename SoundFileName = Left(SoundFileName, InStr(1, SoundFileName, Chr(10)) - 1) End If PlayFileWav SoundFileName, False End Sub
如何创建声音记录:
通过右键单击单元格来插入单元格注释,然后选择“插入注释”。…
填写要在单元格注释的第一句话中播放的声音文件的完整文件名和路径,例如C:\ Foldername \ Soundfilename.wav。
如果要在声音文件名之外添加书面消息,请在文件名后按ENTER键以在注释中创建新句子。将您想要的文本添加到新句子。
宏PlaySoundNotesInExcel97可以由eventmacro Worksheet_SelectionChange()激活,这将在用户每次使用音符激活单元格时使音符播放。