Page 1 of 1

Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 2:25 pm
by Conklin
What would be the code that would need to be inserted
to make a "Beep" prior to ending of a macro?

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 2:32 pm
by SPerman

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:03 pm
by DanPihlaja
Can you get it to make a Boop as well? Beep Boop would be kind of cool. :)

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:09 pm
by Frederick_Law
Bee Boo Bee Boo Bee Boo

Good to know good old Beep still works.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:10 pm
by Conklin
I added this bit Got the message boxes but no Beeps between

MsgBox "beep Start"
Dim x
For x = 1 To 10 ' Loop 10 times.
Beep ' Sound a tone.
Next x
MsgBox "beep done"

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:12 pm
by Roasted By John
dpihlaja wrote: Tue Apr 20, 2021 3:03 pm Can you get it to make a Boop as well? Beep Boop would be kind of cool. :)
I'd go for the Bee Bop

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:13 pm
by Roasted By John
Conklin wrote: Tue Apr 20, 2021 3:10 pm I added this bit Got the message boxes but no Beeps between

MsgBox "beep Start"
Dim x
For x = 1 To 10 ' Loop 10 times.
Beep ' Sound a tone.
Next x
MsgBox "beep done"


The message box is just a prompt for you to yell "BeeP"

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:19 pm
by HerrTick
I recall it was possible to change the tone of the beep.
http://www.vb-helper.com/howto_net_play_tone.html
image.png
image.png (8.01 KiB) Viewed 5111 times

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:46 pm
by mattpeneguy
HerrTick wrote: Tue Apr 20, 2021 3:19 pm I recall it was possible to change the tone of the beep.
http://www.vb-helper.com/howto_net_play_tone.html
image.png
18khz for 5 minutes would be a good starting point. Just make sure to turn the speakers up to 11...

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 3:52 pm
by jcapriotti
mattpeneguy wrote: Tue Apr 20, 2021 3:46 pm 18khz for 5 minutes would be a good starting point. Just make sure to turn the speakers up to 11...
image.png

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 4:07 pm
by Roasted By John
Actually, just waiting for one of my co-workers to come back to his office, just sent him a chat note that had Cat sounds, but I turned his speakers all the way up...... MEOW GRRR

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 4:35 pm
by Conklin
Could it be that in the "Available References:" list for Solidworks it does not have any choice for sounds?
sound ref.jpg
I just thought it would be nice to have the Computer yell at me (Wake me up) when the macro is done.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 6:19 pm
by SPerman
Try just a single beep without the loop.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Tue Apr 20, 2021 9:07 pm
by Conklin
SPerman
Try just a single beep without the loop.
I did to no avail

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 11:07 am
by SPerman
That works on my computer.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 3:20 pm
by Conklin
Post by SPerman
That works on my computer.
I did it again (with out loop) and a single beep command, and yes it makes a sound BUT I think it is just a sound that is a standard sound when a message box is displayed and it, the macro, wants a user input.
Is there a way to have a macro run, pull, or retrieve a sound, say a "wav" file, off the hard drive and play it from within the SW macro?

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 3:32 pm
by AlexB
Conklin wrote: Wed Apr 21, 2021 3:20 pm I did it again (with out loop) and a single beep command, and yes it makes a sound BUT I think it is just a sound that is a standard sound when a message box is displayed and it, the macro, wants a user input.
Is there a way to have a macro run, pull, or retrieve a sound, say a "wav" file, off the hard drive and play it from within the SW macro?
I'm not sure if it even works, but this came up with a quick search about playing sound files with VBA

https://en.wikibooks.org/wiki/Visual_Ba ... e_from_VBA

Edit: I just tested it and it works great. Any WAV file plays with the message box.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 3:40 pm
by SPerman
I found several references for playing a wave file, but it was all pre windows 10, so I wasn't sure if any of them would work.

I guess it is more of a ding than a beep.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 3:56 pm
by mattpeneguy
SPerman wrote: Wed Apr 21, 2021 3:40 pm I found several references for playing a wave file, but it was all pre windows 10, so I wasn't sure if any of them would work.

I guess it is more of a ding than a beep.
I think this is doable, but if the VBA isn't working inside the API to play the sound, it's going to get convoluted. I'm pretty darn sure you can use a string in VBA to call an application and send it a file to play and it'll work...But, man is that messy!

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 3:58 pm
by mattpeneguy
mattpeneguy wrote: Wed Apr 21, 2021 3:56 pm I think this is doable, but if the VBA isn't working inside the API to play the sound, it's going to get convoluted. I'm pretty darn sure you can use a string in VBA to call an application and send it a file to play and it'll work...But, man is that messy!
Ahhh, but if you go the messy method, you absolutely have to use Flight of the Valkyries!

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 10:17 pm
by Conklin
OK so I got the beep to work
Via A " Duck Duck Go" search that turned up a Thread on the "Old Fourm":
What do you think about a Confirmation BEEP?
Asked by Dan Golthing | 05/30/19 |

In it a reply by Thomas Allsup
Where he references a presentation he did I think at SW World:
2008 one minute presentation: "One Minute Tip"

So I came up with this code:

Code: Select all

    MsgBox "beep Start"
    Start = 0
    Pause = 10  ' Pause
    Start = Timer ' Set start time.
        Do While Timer < Start + Pause
        Beep
        Loop
    MsgBox "beep done"
He also referenced some on how to play a wave file"

Code: Select all

Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Sub PlaySound()

    Call sndPlaySound32("c:\solidworks.wav", 0)

End Sub

Call PlaySound
Now my next challenge is to get the above to work and then... "Flight of the Valkyries!"

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Wed Apr 21, 2021 10:27 pm
by Conklin
by AlexB
https://en.wikibooks.org/wiki/Visual_Ba ... e_from_VBA

Edit: I just tested it and it works great. Any WAV file plays with the message box.
I will try this. Thanks

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Thu Apr 22, 2021 8:02 am
by DanPihlaja
Conklin wrote: Tue Apr 20, 2021 4:35 pm Could it be that in the "Available References:" list for Solidworks it does not have any choice for sounds?
sound ref.jpg
I just thought it would be nice to have the Computer yell at me (Wake me up) when the macro is done.
Just configure your sounds inside Solidworks and have the last line of your macro do something from the list.
Might make it cumbersome, though. But if the last line of your macro is a rebuild, then configure the rebuild complete sound to whatever you want.
image.png

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Thu Apr 22, 2021 8:07 am
by mike miller
dpihlaja wrote: Thu Apr 22, 2021 8:02 am Just configure your sounds inside Solidworks and have the last line of your macro do something from the list.
Might make it cumbersome, though. But if the last line of your macro is a rebuild, then configure the rebuild complete sound to whatever you want.

image.png
I don't think Flight of the Valkyries is available from that list. :lol:

Good idea though. :D

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Thu Apr 22, 2021 9:11 am
by Conklin
by dpihlaja » Thu Apr 22, 2021 8:02 am

Just configure your sounds inside Solidworks and have the last line of your macro do something from the list.
Might make it cumbersome, though. But if the last line of your macro is a rebuild, then configure the rebuild complete sound to whatever you want.
Good idea. When I get time I'll try that too. Thanks

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Thu Apr 22, 2021 9:15 am
by HerrTick
There's a trick to going up the chromatic scale.

Each half-step's frequency is 2^(1/12) times greater than the previous. You can extend this ad infinitum.
Take A=440Hz
B is two half-steps higher --> B = A * 2^(2/12)
C is 3 half-steps greater --> C = A * 2^(3/12)

Note an octave's frequency is double the root. A2 = A1 * 2^(12/12) = A1 * 2^1 = A1*2

Logarithms!!! =) tu
image.png
image.png (44.32 KiB) Viewed 4994 times

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Thu Apr 22, 2021 11:02 pm
by Conklin
by AlexB

I'm not sure if it even works, but this came up with a quick search about playing sound files with VBA

https://en.wikibooks.org/wiki/Visual_Ba ... e_from_VBA

Edit: I just tested it and it works great. Any WAV file plays with the message box.
I did mark you as the correct answer... based on your "Edit: I just tested it and it works great."
BUT
I tried to make it work as well but after several hours I give up.
Alex, if you could post the code you used to test.... that would be greatly appreciated.
I am doing something wrong. I am just knowledgeable enough to be dangerous with API.
but I can follow an example... and paste it in to a macro I am using... I think...

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Fri Apr 23, 2021 2:52 pm
by Conklin
OK so I dropped the below code just as it was on the web page
into a new SW macro and and it works great
I guess I was trying to think too much for I was trying to put it into/at the end of an existing macro
and could not get it to work.

Code: Select all

Option Explicit

'Declaration for Win10 and Office 64 bit
Public Declare PtrSafe Function sndPlaySound Lib "winmm.dll" _
        Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
           ByVal uFlags As Long) As Long

Sub TestPlayWavFileAPI()
    'run this to play a sound wave (.wav) file
    
    Dim sPath As String
    
    'path to wave file - replace with your own
    sPath = "C:\Windows\Media\Ring08.wav"
    
    'test the no-wait feature
    PlayWavFileAPI sPath, False
    MsgBox "This message box appears during the sound"
    
    'test the wait feature
    PlayWavFileAPI sPath, True
    MsgBox "This message appears only after the sound stops"

End Sub

Function PlayWavFileAPI(sPath As String, Wait As Boolean) As Boolean
    'API declaration of sndPlaySound is modified for 64 bit windows
    'and tests well in Excel 2019 vba version 7.1.
    'For earlier versions it might be necessary to remove the word PtrSafe
    'from the declaration, or to consider another format.
    
    'make sure file exists
    If Dir(sPath) = "" Then
        Exit Function
    End If
    
    If Wait Then
        'hold up follow-on code until sound complete
        sndPlaySound sPath, 0
    Else
        'continue with code run while sound is playing
        sndPlaySound sPath, 1
    End If

End Function
Now I just figured out / or better said happen to place the end sub in the right location
And now it works Thanks All.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Fri Apr 23, 2021 3:39 pm
by Conklin
NOW
Where do I find, I think it is, "The ride of the Valkyries"
In a "WAV" format !!!!!!
Can an "MP3" be converted?
Or
Do I now have to figure out how to get a "MP3" to work? o[

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Fri Apr 23, 2021 3:55 pm
by mattpeneguy
Conklin wrote: Fri Apr 23, 2021 3:39 pm NOW
Where do I find, I think it is, "The ride of the Valkyries"
In a "WAV" format !!!!!!
Can an "MP3" be converted?
Or
Do I now have to figure out how to get a "MP3" to work? o[
Check your Private Messages.

Re: Would like a sound, a "Beep", prior to or at the end of a macro

Posted: Mon Apr 26, 2021 9:31 am
by HerrTick
If you want to use sound files, you would do well to learn about resource modules in VB. A resource module lets you store files for access by the program, without having to have the file reside at a specific address on your drive.

disclaimer: My API kung fu is old. This is VB6 or VBA circa 2010. It appears VB.NET has as well.

https://www.thoughtco.com/vbnet-resource-files-3424443