{"id":55393,"date":"2008-06-10T02:04:00","date_gmt":"2008-06-10T02:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/06\/10\/hey-scripting-guy-how-can-i-apply-a-timeout-to-an-inputbox\/"},"modified":"2008-06-10T02:04:00","modified_gmt":"2008-06-10T02:04:00","slug":"hey-scripting-guy-how-can-i-apply-a-timeout-to-an-inputbox","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-apply-a-timeout-to-an-inputbox\/","title":{"rendered":"Hey, Scripting Guy! How Can I Apply a Timeout to an InputBox?"},"content":{"rendered":"<p><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hey, Scripting Guy! I know that, by using the WSH Popup method, you can time out a message box. Is it possible to do something similar with the InputBox function?<br \/>&#8212; JR<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, JR. Greetings from sunny Florida! Well, at least we <i>assume<\/i> it\u2019s sunny Florida; it\u2019s late Sunday night as the Scripting Guy who writes this column writes this column, and, at the moment, it\u2019s pretty dang dark outside. However, it will soon be sunny again; after all, this is Florida, and it pretty much <i>has<\/i> to be sunny. Otherwise they have to give us our money back.<\/p>\n<p>So far the Scripting Guys\u2019 trip to TechEd Orlando is off to a good start: we spent most of our first day here researching the rides at Universal Studios <a href=\"http:\/\/www.universalorlando.com\/ioa_index.html\" target=\"_blank\"><b>Islands<\/b><b> of Adventure<\/b><\/a>. (Research is a very important part of being a Scripting Guy.) On top of that, we made an initial effort to determine if they really <i>do<\/i> sell margaritas at <a href=\"http:\/\/www.margaritavilleorlando.com\/\" target=\"_blank\"><b>Jimmy Buffet\u2019s Margaritaville<\/b><\/a>. Those results were inconclusive, so we may have to go back and do additional research.<\/p>\n<p>If you know what we mean.<\/p>\n<p>As much fun as Sunday was, however, today promises to be an even <i>more<\/i> exciting day: today we\u2019re going to go register for the conference; we\u2019re going to do a \u201cTech Check\u201d for our instructor-led lab; and we\u2019re going to try to track down the boxes full of handouts and bobbleheads that we shipped to Orlando. Jealous? Understood<\/p>\n<p>Although, the more we think about it, the less fun this all sounds; the truth is, it sounded <i>way<\/i> better in the guide book than it does now. And to think that we gave up a trip to Sea World for <i>that<\/i>.<\/p>\n<p>Oh, well; easy come, easy go. In the meantime we have a question to answer. And here\u2019s your answer, JR: as far as we know there\u2019s no way to apply a timeout value to an InputBox. No way at all.<\/p>\n<p>However (even when there\u2019s no answer there\u2019s almost always a however), there <i>is<\/i> a way that we can mimic this behavior: we can use an instance of Internet Explorer to stand in for the InputBox, and then apply a timeout to that instance of Internet Explorer. It\u2019s not the most elegant solution in the world; for one thing, you\u2019ll need two files (your script and a .HTM file). However (there\u2019s that word again), it should do the trick.<\/p>\n<p>So <i>how<\/i> will it do the trick? Before we explain that, let\u2019s take a peek at the HTML file we need to create (C:\\Scripts\\Test.htm):<\/p>\n<pre class=\"codeSample\">&lt;SCRIPT language=\"VBScript\"&gt;\n    Sub OKClicked\n        ButtonClicked.Value = BasicTextBox.Value\n   End Sub\n\n    Sub CancelClicked\n        ButtonClicked.Value = \"Cancelled\"\n    End Sub\n&lt;\/SCRIPT&gt;\n\n&lt;BODY&gt;\n    &lt;input type=\"text\" name=\"BasicTextBox\" size=\"50\" Value=\"Default value\"&gt;&lt;P&gt;\n    &lt;input type=\"Button\" value=\"OK\" onClick=\"OKClicked\"&gt;\n    &lt;input type=\"Button\" value=\"Cancel\" onClick=\"CancelClicked\"&gt;\n    &lt;input type=\"hidden\" name=\"ButtonClicked\"&gt;\n&lt;\/BODY&gt;\n<\/pre>\n<p>We\u2019re not going to explain the HTML tagging in any detail today; that goes a bit beyond what we can do in a single column. Suffice to say that we have an HTML page that features a text box (the place where the user enters his or her input); an <b>OK<\/b> button and a <b>Cancel<\/b> button; and a hidden field. When this page pops up onscreen, the user can enter data into the text box (which has a default value of, well, <i>Default value<\/i>) and then click <b>OK<\/b>; alternatively, he or she can simply click <b>Cancel<\/b>. Either way, a value is assigned to the hidden field. What our .VBS file does is monitor the value of that hidden field. As soon as it <i>has<\/i> a value, the script closes the HTML page and then moves on to bigger and better things. Onscreen, our little HTML page will look something like this:<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Internet Explorer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/ieinput.jpg\" width=\"400\" height=\"250\" \/> <\/p>\n<p><\/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>Note<\/b>. Yes, <i>you<\/i> could create a page that looks a little more like an InputBox. We were too lazy \u2013 and still a bit damp from Popeye and Bluto\u2019s Bilge Rat Barges \u2013 to do that ourselves.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Ah good point: how <i>do<\/i> we apply a timeout to this HTML page? Tell you what, let\u2019s take a look at our actual script code, and then see if we can figure that out:<\/p>\n<pre class=\"codeSample\">On Error Resume Next\n\nSet objExplorer = CreateObject(\"InternetExplorer.Application\")\n\nobjExplorer.Navigate \"file:\/\/\/c:\\scripts\\test.htm\"\nobjExplorer.ToolBar = 0\nobjExplorer.StatusBar = 0\nobjExplorer.Width = 400\nobjExplorer.Height = 250\nobjExplorer.Visible = 1\n\ni = 0\n\nDo While (objExplorer.Document.All.ButtonClicked.Value = \"\")\n    Wscript.Sleep 250\n    i = i + 1\n    If i = 40 Then\n        Exit Do\n    End If    \nLoop\n\nstrValue = objExplorer.Document.All.ButtonClicked.Value\n\nIf strValue = \"\" Then\n    strValue = \"Default value\"\nEnd If\n\nobjExplorer.Quit\nWscript.Sleep 250\n\nWscript.Echo strValue\n<\/pre>\n<p>As you can see, we start things off by creating an instance of the <b>InternetExplorer.Application<\/b> object; that\u2019s the object that lets us create \u2013 and manipulate \u2013 our very own instance of Internet Explorer. After we create the object, we use the <b>Navigate<\/b> method to open our \u201cinput box\u201d (the file C:\\Scripts\\Test.htm):<\/p>\n<pre class=\"codeSample\">objExplorer.Navigate \"file:\/\/\/c:\\scripts\\test.htm\"\n<\/pre>\n<p>We then assign values to several properties of our Internet Explorer instance: we set the <b>ToolBar<\/b> and <b>StatusBar<\/b> properties to 0 (which hides the tool bar and status bar from view), we define both the <b>Width <\/b>and <b>Height<\/b> of the Internet Explorer window. In addition, we set the <b>Visible<\/b> property to True. That\u2019s important, by the way: if you don\u2019t do that, you won\u2019t be able to see your Web page. And let\u2019s face it, an invisible Web page is of little use to anyone.<\/p>\n<p>OK, <i>maybe<\/i> the Invisible Man. But no one else that we know of.<\/p>\n<p>After assigning the value 0 to a counter variable named i we then hit this block of code:<\/p>\n<pre class=\"codeSample\">Do While (objExplorer.Document.All.ButtonClicked.Value = \"\")\n    Wscript.Sleep 250\n    i = i + 1\n    If i = 40 Then\n        Exit Do\n    End If    \nLoop\n<\/pre>\n<p>You know, we had the very same question: what <i>is<\/i> going on here? What we\u2019ve done here is set up a Do While loop that continues to run until our hidden field (<b>objExplorer.Document.All.ButtonClicked<\/b>) has a value. (When the HTML page first loads up, this field does <i>not<\/i> have a value.) What the script does is check to see if the <b>Value<\/b> of our hidden field is an empty string (\u201c\u201d). If it <i>is<\/i> empty, then the script sleeps for one-quarter of a second (250 milliseconds). Once that brief pause is over, we then increment our counter variable i by 1.<\/p>\n<p>Why? Well, we\u2019ve decided to apply a 10-second timeout to the HTML page: if 10 seconds have elapsed and the user still hasn\u2019t entered something then we\u2019re going to automatically dismiss the page. Admittedly, that <i>sounds<\/i> pretty straightforward; on the other hand, spending the day searching for boxes at the Convention Center <i>sounded<\/i> pretty exciting, too. For better or worse, however, it\u2019s a little trickier than that. For example, we could just pause the script for 10 seconds and then proceed from there. That would work, but it wouldn\u2019t make for a very good user experience. <\/p>\n<p>Why not? Well, suppose the HTML page appears and the user immediately clicks <b>Cancel<\/b>. Despite that rapid response, however, nothing will happen until the 10-second timeout has elapsed: the user will click <b>Cancel<\/b>, but it will be 10 seconds before the HTM page disappears.<\/p>\n<p>Fortunately, though, our approach ensures that only a split second (250 milliseconds) elapses before the HTML page disappears. We check the value of the hidden field, then pause for 250 milliseconds. We increment our counter variable by 1, then check to see if this counter variable is equal to 40. Why 40? Well, remember, we want to set a 10-second timeout on this page. If we pause for one-quarter of a second at a time, 40 such pauses will add up to 10 seconds. (40 x 250 = 10,000, and 10,000 divided by 1,000 equals 1.) If i equals 40, that means we\u2019ve reached the 10-second time limit. As a result, we call the <b>Exit Do<\/b> statement to exit the loop.<\/p>\n<p>Of course, if i <i>isn\u2019t<\/i> equal to 40 we simply go back to the top of the loop and repeat the process, checking to see if the hidden field has a value. If this field <i>does<\/i> have a value, then we automatically exit the loop; after all, our Do loop is designed to function only as long as the hidden field has <i>no<\/i> value.<\/p>\n<p>Sooner or later we\u2019ll eventually exit the Do loop, either because the user entered a value and clicked <b>OK<\/b>, the user clicked <b>Cancel<\/b>, or the HTML page timed out. As soon as we exit the loop we use this line of code to grab the value of the hidden field and store it in a variable named strValue:<\/p>\n<pre class=\"codeSample\">strValue = objExplorer.Document.All.ButtonClicked.Value\n<\/pre>\n<p>That\u2019s nice, but what if the HTML page timed-out? After all, in that case, the hidden field won\u2019t <i>have<\/i> a value. Consequently, we use this chunk of code to assign it the default value <i>Default value<\/i>:<\/p>\n<pre class=\"codeSample\">If strValue = \"\" Then\n    strValue = \"Default value\"\nEnd If\n<\/pre>\n<p>From there we call the <b>Quit<\/b> method to terminate our instance of Internet Explorer, then pause the script 250 milliseconds, giving Internet Explorer a chance to disappear from screen. At that point we can echo back the value of the variable strValue and then call it a day:<\/p>\n<pre class=\"codeSample\">Wscript.Echo strValue<\/pre>\n<p>Elegant? Maybe not. But, like we said, it works.<\/p>\n<p>We hope that answers your question, JR, because that\u2019s the best we can do for now; it\u2019s time for the Scripting Guy who writes this column to get some sleep. After all, he has a busy day ahead of him, and a busy day behind him: riding theme park rides all day is more tiring than you might think. Still, while it might have been tiring it was a good day all around. Well, maybe not for the Scripting Editor. She enjoyed the rides and everything, but we\u2019re not sure if she liked all the people who came up to her and, motioning towards the Scripting Guy who writes this column, remarked on the fact that \u201c\u2026 it\u2019s just so wonderful that you brought your grandson to Islands of Adventure!\u201d And hey, don\u2019t think the Scripting Guy who writes this column doesn\u2019t appreciate that, too \u2026 Grandma. <i>(<\/i><i>Ed<\/i><i>itor\u2019s Note: Obviously the Scripting Guy who writes this column was already asleep and dreaming when he wrote that last part. Goodnight Gramps.)<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I know that, by using the WSH Popup method, you can time out a message box. Is it possible to do something similar with the InputBox function?&#8212; JR Hey, JR. Greetings from sunny Florida! Well, at least we assume it\u2019s sunny Florida; it\u2019s late Sunday night as the Scripting Guy who writes [&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":[3,4,5,30],"class_list":["post-55393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I know that, by using the WSH Popup method, you can time out a message box. Is it possible to do something similar with the InputBox function?&#8212; JR Hey, JR. Greetings from sunny Florida! Well, at least we assume it\u2019s sunny Florida; it\u2019s late Sunday night as the Scripting Guy who writes [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55393","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=55393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55393\/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=55393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}