声明在用户窗体中播放声音的功能

时间:2018-11-19 15:59:34

标签: excel vba excel-vba 32bit-64bit function-declaration

好的,所以我有一个Excel工具,可以打开用户窗体,并可以根据选择播放声音,并要求用户选择音源。在32位上可以很好地工作,但是最近我更新为64位,并了解到代码并不完全相同。

我的原始代码是这样的,它位于UserForm(一般声明)上的所有Subs之外:

    Declare Function PlaySound Lib "winmm.dll" _
      Alias "PlaySoundA" (ByVal lpszName As String, _
      ByVal hModule As Long, ByVal dwFlags As Long) As Long

我一直在网上四处浏览,并看到了很多有关LongPtr的PtrSafe函数和变量的内容,因此我尝试根据自己在网上看到的示例使用此代码:

#If VBA7 And Win64 Then
Declare PtrSafe Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As StrPtr, _
  ByVal hModule As LongPtr, ByVal dwFlags As LongPtr) As LongPtr
#Else
Declare Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As String, _
  ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If

我得到的错误是“编译错误:未定义用户定义的类型”,突出显示了#If VBA7 And Win64 Then下的这部分代码...

Declare PtrSafe Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As StrPtr, _
  ByVal hModule As LongPtr, ByVal dwFlags As LongPtr) As LongPtr

我完全不知所措。是否有我需要核对的参考文献?当其他人使用该工具时,会引起兼容性问题吗?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

StrPtr是一个函数,而不是数据类型。您只需要:

#If VBA7 Then
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As String, _
  ByVal hModule As LongPtr, ByVal dwFlags As Long) As Long
#Else
Private Declare Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As String, _
  ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If