在用户窗体中声明播放声音的函数

Declare Function to Play Sound in a UserForm

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

我的原始代码是这样的,位于用户窗体(一般声明)上的任何 Subs 之外:

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

我一直在网上四处寻找,看到很多关于 PtrSafe 函数和变量的东西,如 LongPtr,所以我尝试根据我在网上看到的示例使用此代码:

#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

我得到的错误是 "Compile Error: User-defined type not defined",它突出显示了 #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

我完全不知所措。是否有我不知道的需要核对的参考资料?当其他人使用该工具时,这会导致兼容性问题吗?感谢任何帮助。

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