{"id":64323,"date":"2007-08-02T01:15:00","date_gmt":"2007-08-02T01:15:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/02\/how-can-i-play-a-sound-each-time-a-message-box-is-displayed\/"},"modified":"2007-08-02T01:15:00","modified_gmt":"2007-08-02T01:15:00","slug":"how-can-i-play-a-sound-each-time-a-message-box-is-displayed","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-play-a-sound-each-time-a-message-box-is-displayed\/","title":{"rendered":"How Can I Play a Sound Each Time a Message Box is Displayed?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! I was using a script written by one of my colleagues, and I noticed that any time this script displayed a message box a sound was played as well. How can I play a sound each time I display a message box?<BR><BR>&#8212; KI <\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, KI. You\u2019ll have to give us a second here; the Scripting Guy who writes this column got home a little late last night, having ventured into the wilds of Seattle to watch the Mariners play the Angels. (And yes, now that the Scripting Son\u2019s baseball season is finally over, he and his father celebrated by, uh, going to a baseball game.) As it turned out, however, the two saw a pretty good game: not only did the Mariners win 2-0, but the Scripting Guy who writes this column correctly predicted that the green hydroplane would win the animated Hydro Challenge. Not a bad evening no matter how you look at it.<\/P>\n<P>In addition, father and son also had the pleasure of sitting in front of a guy who knew <I>everything<\/I> about baseball, and who kept up a non-stop commentary all night long. To give you a sample of what the night was like, here are a few of his more interesting pronouncements:<\/P>\n<P>\u201cRichie Sexson, now there\u2019s a guy who was really hurt by the crackdown on steroids. He had that one year where he hit 75 home runs, but after the crackdown, nothing. Not even close.\u201d <\/P>\n<TABLE id=\"EBD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019re wondering, Richie Sexson has never hit 75 home runs in a season; in fact, the record for most home runs in a season (held by Barry Bonds) is 73. The best Richie Sexson ever did was hit 45 homers in one year, something he accomplished twice while playing for the Milwaukee Brewers.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>\u201cHank Aaron was the only clean baseball player, ever. Everyone before him and after him was all drugged-up. They\u2019re all a bunch of liars, cheats, and thieves.\u201d<\/P>\n<P>\u201cOh, sure, baseball executives, politicians, businessmen \u2013 they all have to pretend that the problem never existed. If they owned up to the fact that the players have all been taking steroids then they\u2019d have to give all the fans their money back, and they\u2019d have to refund all the money paid out for advertising and TV rights and everything else. That would wreck the entire economy, absolutely wreck it. Because of that, everyone pretends that there\u2019s no such thing as steroids. That\u2019s why they\u2019ve never cracked down on any of this stuff.\u201d<\/P>\n<TABLE id=\"EOD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. For an interesting comparison, see the first quote, the one about baseball having cracked down on steroids.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And, of course, our very favorite:<\/P>\n<P>\u201cCome on, Beltre; hit the ball! You know what? I think Adrian Beltre needs Viagra for his batting brain!\u201d<\/P>\n<P>No, we have no idea what that means, either. Send your suggestions to <A href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com (in English, if possible)<\/A>.<\/P>\n<P>On second thought, don\u2019t bother; all things considered, we\u2019d rather <I>not<\/I> know what that means.<\/P>\n<P>In all fairness, however, we must admit that the guy did say at least one thing that made a lot of sense:<\/P>\n<P>\u201cCome on, blue: open your eyes! Don\u2019t you know that you can play a sound any time a message box appears simply by including an icon in that message box?\u201d<\/P>\n<P>In other words:<\/P><PRE class=\"codeSample\">Msgbox &#8220;An error has occurred. Do you want to continue?&#8221;, _\n    vbYesNo + vbCritical, &#8220;Path Not Found&#8221;\n<\/PRE>\n<P>So did this guy <I>really<\/I> know what he was talking about? Well, let\u2019s find out. As you can see, in this example we\u2019ve simply called the <B>Msgbox<\/B> function and passed in three parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cAn error has occurred. Do you want to continue?\u201d<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>vbYesNo + vbCritical<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>\u201cPath Not Found\u201d<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>The first parameter is easy to figure out; that\u2019s simply the message we want displayed in the message box. The third parameter is also pretty straightforward: that\u2019s the title we want to assign to our message box. That leaves us with this: <B>vbYesNo + vbCritical<\/B>.<\/P>\n<P>As it turns out, we\u2019re actually defining two things here. First, we\u2019re using the VBScript constant vbYesNo to indicate that we want to include a <B>Yes<\/B> button and a <B>No<\/B> button in our message box. Are we limited to just Yes and No buttons in a message box? Of course not; here are all the button combinations available to you:<\/P>\n<TABLE id=\"EHF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Buttons Displayed<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbOKOnly<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">OK<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbOKCancel<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">OK and Cancel<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbAbortRetryIgnore<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Abort, Retry, and Ignore<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbYesNoCancel<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Yes, No, and Cancel<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbYesNo<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Yes and No<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbRetryCancel<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Retry and Cancel<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>But notice that we aren\u2019t just defining the set of buttons to be displayed in our message box; we\u2019re also defining an icon to be displayed. That\u2019s what the constant vbCritical is for; it displays a Critical Message icon (a big red circle with an X in it) in the message box. You know, like this:<\/P><IMG border=\"0\" alt=\"Message Box\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/msgbox.jpg\" width=\"311\" height=\"126\"> \n<P><BR>Ah, but that\u2019s not all. When you assign an icon you get the icon, sure, but you also get the system sound associated with the event (in this case, Critical Message). <I>That\u2019s<\/I> how you can play a sound each time a message box is displayed: just be sure and assign an icon to the message box. Don\u2019t like either the sound or the icon for Critical Message? Then try one of these constants instead:<\/P>\n<TABLE id=\"E6G\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Icon Displayed\/Sound Played<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbCritical<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Critical Message<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbQuestion<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Warning Query<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbExclamation<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Warning Message<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">vbInformation<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Information Message<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>See how this works? Suppose you wanted to display <B>OK <\/B>and <B>Cancel<\/B> buttons along with the Warning Message icon (and sound). That\u2019s fine; in that case your call to the Msgbox function would look like this:<\/P><PRE class=\"codeSample\">Msgbox &#8220;An error has occurred. Do you want to continue?&#8221;, _\n    vbOKCancel + vbExclamation, &#8220;Path Not Found&#8221;\n<\/PRE>\n<P>Etc.<\/P>\n<P>Here\u2019s another nifty trick. Try adding the constant <B>vbSystemModal<\/B> to the Msgbox function:<\/P><PRE class=\"codeSample\">Msgbox &#8220;An error has occurred. Do you want to continue?&#8221;, _\n    vbOKCancel + vbExclamation + vbSystemModal, &#8220;Path Not Found&#8221;\n<\/PRE>\n<P>Why would you want to do that? Well, if you make this a system modal message box, then the message box remains on top of all your other windows, even if you switch to a different application. (You can still use that application, but the message box remains on top. Give it a try and you\u2019ll see what we mean.) Is that useful? It can be; if nothing else, it makes it a little bit harder for users to ignore your message box. <\/P>\n<P>Before you ask, yes, you <I>are<\/I> limited to those four icons and the system sounds associated with those events. (And again, before you ask, there\u2019s really no way to change those system sounds programmatically.) If you\u2019d rather play a <I>different<\/I> sound each time a message box is displayed then you might want to take a look at one of our previous <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1103.mspx\"><B>Hey, Scripting Guy! columns<\/B><\/A>. The method employed there is far from foolproof, but you might find it useful. Give it a try and see for yourself.<\/P>\n<P>That should do it, KI. As for the baseball game, we\u2019ll let the Scripting Son have the last word. Mystified by a pair of ball and strike calls, the Scripting Son sighed in exasperation. \u201cCome on, blue,\u201d he called to the ump. \u201cTry turning home plate over and reading the instructions.\u201d Although the Scripting Guy who writes this column has hung around baseball fields all his life, he\u2019d never heard that one before.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I was using a script written by one of my colleagues, and I noticed that any time this script displayed a message box a sound was played as well. How can I play a sound each time I display a message box?&#8212; KI Hey, KI. You\u2019ll have to give us a second [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25,3,4,5],"class_list":["post-64323","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-displaying-output","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I was using a script written by one of my colleagues, and I noticed that any time this script displayed a message box a sound was played as well. How can I play a sound each time I display a message box?&#8212; KI Hey, KI. You\u2019ll have to give us a second [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64323"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64323\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}