如何使用VBA播放波形文件或声音文件
答
这是WAV文件的一种方法。把** ** this code在定期代码模块:
Option Explicit
Public Declare Function sndPlaySound32 _
Lib "winmm.dll" _
Alias "sndPlaySoundA" (_
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Sub PlayTheSound(ByVal WhatSound As String)
If Dir(WhatSound, vbNormal) = "" Then
' WhatSound is not a file. Get the file named by
' WhatSound from the Windows\Media directory.
WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
If InStr(1, WhatSound, ".") = 0 Then
' if WhatSound does not have a .wav extension,
' add one.
WhatSound = WhatSound & ".wav"
End If
If Dir(WhatSound, vbNormal) = vbNullString Then
Beep ' Can't find the file. Do a simple Beep.
Exit Sub
End If
Else
' WhatSound is a file. Use it.
End If
sndPlaySound32 WhatSound, 0& ' Finally, play the sound.
End Sub
现在,你可以通过任何其他宏观播放任何wav文件通过调用高于常规,在/ Media文件夹中找到的任何文件名喂养:
Sub PlayIt()
Select Case Range("A1").Value
Case "good"
PlayTheSound "chimes.wav"
Case "bad"
PlayTheSound "chord.wav"
Case "great"
PlayTheSound "tada.wav"
End Select
End Sub
玩弄那个。
下面是一个示例文件,我用它来极大地显示了无规名称和任务的日子,它是连接到一个按钮,像你打算做:
感谢添加链接,布雷特!我的坏... – 2012-02-25 23:39:56
没问题,这是一个整容更新。 +1 btw – brettdj 2012-02-26 02:19:40