Declare Function to Play Sound in a UserForm [closed]
up vote
0
down vote
favorite
Okay so I had an Excel tool that would open a userform and have the option to play a sound based on selections and requiring the user to select the source of the tones. This was working beautifully in 32-bit, but I recently updated to 64-bit and learned that the code is not all the same.
My original code was this, sitting outside of any Subs on the UserForm (General Declarations):
Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
I have been looking around online and seen a lot of stuff about PtrSafe Functions and variables as LongPtr so I tried using this code based on what I've seen samples of online:
#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
The error I'm getting is "Compile Error: User-defined type not defined" which highlights this part of the code under #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
I'm at a complete loss. Is there a reference that I need to check off that I'm not aware of? Will that cause compatibility issues when someone else uses the tool? Any help is appreciated.
excel vba excel-vba 32bit-64bit function-declaration
closed as off-topic by K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub Nov 19 at 18:48
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
Okay so I had an Excel tool that would open a userform and have the option to play a sound based on selections and requiring the user to select the source of the tones. This was working beautifully in 32-bit, but I recently updated to 64-bit and learned that the code is not all the same.
My original code was this, sitting outside of any Subs on the UserForm (General Declarations):
Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
I have been looking around online and seen a lot of stuff about PtrSafe Functions and variables as LongPtr so I tried using this code based on what I've seen samples of online:
#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
The error I'm getting is "Compile Error: User-defined type not defined" which highlights this part of the code under #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
I'm at a complete loss. Is there a reference that I need to check off that I'm not aware of? Will that cause compatibility issues when someone else uses the tool? Any help is appreciated.
excel vba excel-vba 32bit-64bit function-declaration
closed as off-topic by K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub Nov 19 at 18:48
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Okay so I had an Excel tool that would open a userform and have the option to play a sound based on selections and requiring the user to select the source of the tones. This was working beautifully in 32-bit, but I recently updated to 64-bit and learned that the code is not all the same.
My original code was this, sitting outside of any Subs on the UserForm (General Declarations):
Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
I have been looking around online and seen a lot of stuff about PtrSafe Functions and variables as LongPtr so I tried using this code based on what I've seen samples of online:
#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
The error I'm getting is "Compile Error: User-defined type not defined" which highlights this part of the code under #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
I'm at a complete loss. Is there a reference that I need to check off that I'm not aware of? Will that cause compatibility issues when someone else uses the tool? Any help is appreciated.
excel vba excel-vba 32bit-64bit function-declaration
Okay so I had an Excel tool that would open a userform and have the option to play a sound based on selections and requiring the user to select the source of the tones. This was working beautifully in 32-bit, but I recently updated to 64-bit and learned that the code is not all the same.
My original code was this, sitting outside of any Subs on the UserForm (General Declarations):
Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
I have been looking around online and seen a lot of stuff about PtrSafe Functions and variables as LongPtr so I tried using this code based on what I've seen samples of online:
#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
The error I'm getting is "Compile Error: User-defined type not defined" which highlights this part of the code under #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
I'm at a complete loss. Is there a reference that I need to check off that I'm not aware of? Will that cause compatibility issues when someone else uses the tool? Any help is appreciated.
excel vba excel-vba 32bit-64bit function-declaration
excel vba excel-vba 32bit-64bit function-declaration
asked Nov 19 at 15:59
Reece
397
397
closed as off-topic by K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub Nov 19 at 18:48
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub Nov 19 at 18:48
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – K.Dᴀᴠɪs, Sotirios Delimanolis, Masoud, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
StrPtr
is a function, not a data type. All you need is:
#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
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
3
@Reece You should just be able to change toPrivate Declare ...
on both of yourDeclare Function
statements.
– K.Dᴀᴠɪs
Nov 19 at 17:40
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
StrPtr
is a function, not a data type. All you need is:
#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
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
3
@Reece You should just be able to change toPrivate Declare ...
on both of yourDeclare Function
statements.
– K.Dᴀᴠɪs
Nov 19 at 17:40
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
add a comment |
up vote
3
down vote
accepted
StrPtr
is a function, not a data type. All you need is:
#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
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
3
@Reece You should just be able to change toPrivate Declare ...
on both of yourDeclare Function
statements.
– K.Dᴀᴠɪs
Nov 19 at 17:40
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
StrPtr
is a function, not a data type. All you need is:
#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
StrPtr
is a function, not a data type. All you need is:
#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
edited Nov 20 at 13:40
Reece
397
397
answered Nov 19 at 16:07
Rory
23.6k51722
23.6k51722
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
3
@Reece You should just be able to change toPrivate Declare ...
on both of yourDeclare Function
statements.
– K.Dᴀᴠɪs
Nov 19 at 17:40
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
add a comment |
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
3
@Reece You should just be able to change toPrivate Declare ...
on both of yourDeclare Function
statements.
– K.Dᴀᴠɪs
Nov 19 at 17:40
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
I had actually tried that code as well but I get a different error: "Compile Error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules." I thought this might have something to do with the fact that the code is located inside the UserForm, but I tried putting it in the public space of the Workbook code and get the same issue. It actually won't even let me save when it's in the Workbook code.
– Reece
Nov 19 at 17:24
3
3
@Reece You should just be able to change to
Private Declare ...
on both of your Declare Function
statements.– K.Dᴀᴠɪs
Nov 19 at 17:40
@Reece You should just be able to change to
Private Declare ...
on both of your Declare Function
statements.– K.Dᴀᴠɪs
Nov 19 at 17:40
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
That solved the issue and has been tested on 32 and 64 bit. I've edited the answer to include Private as part of the Declare statements.
– Reece
Nov 20 at 13:25
add a comment |