【发布时间】:2017-07-13 15:59:53
【问题描述】:
我有以下 Excel 场景:
A1 的值为“A”,B2 = “B”,C3 = “C”
但它们是可以互换的。
在工作簿路径中有两个包含名为 A.wav、B.wav 和 C.wav 的 wav 文件的子文件夹
底部的代码允许我通过单击触发宏 PlayIt() 的按钮来播放 wav 文件。
我的问题是,当函数正在执行时,我无法在 Excel 中编辑我真正需要的单元格!好像是这样的
https://gifyu.com/images/GIF8d5e1.gif
感谢您的帮助!
音频播放代码:
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
#Else
Public Declare Function PlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
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 = ThisWorkbook.Path & "\stimuli\" & "\1second\" & 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.
MsgBox "Could not find the file in the Path: " & WhatSound
Exit Sub
End If
Else
' WhatSound is a file. Use it.
End If
PlaySound WhatSound, 0&, SND_ASYNC Or SND_FILENAME ' Finally, play the sound.
End Sub
Sub PlayIt()
PlayTheSound (Range("A1").Value)
PlayTheSound (Range("B1").Value)
PlayTheSound (Range("C1").Value & "e")
End Sub
【问题讨论】:
-
ByVal hModule As LongPtr。如果您的办公室是 x64,这可能就是原因。 -
@GSerg 如果我删除
ByVal hModule As LongPtr并且只调用PlaySound WhatSound, SND_ASYNC Or SND_FILENAME我只会听到最后一个 wav 文件PlayTheSound (Range("C1").Value & "e") -
你有
ByVal hModule As Long。正确的是ByVal hModule As LongPtr。 -
@AshtonMorgan 总的来说,这是一个非常糟糕的建议,对于可以本机异步运行的函数尤其糟糕,并且在这种情况下无法实现,因为您需要注入
DoEvents调用内部调用PlaySound。