{"id":66183,"date":"2006-10-25T11:32:00","date_gmt":"2006-10-25T11:32:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/25\/how-can-i-repeatedly-prompt-a-user-to-enter-a-file-name\/"},"modified":"2006-10-25T11:32:00","modified_gmt":"2006-10-25T11:32:00","slug":"how-can-i-repeatedly-prompt-a-user-to-enter-a-file-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-repeatedly-prompt-a-user-to-enter-a-file-name\/","title":{"rendered":"How Can I Repeatedly Prompt a User to Enter a File Name?"},"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 have approximately 100 Excel files (all with names like 9365.xls) in a folder. I\u2019d like to prompt a user for a file name, open the file, retrieve information from a specified cell, then close the file (ideally Excel would never appear on screen). I\u2019d then like to prompt the user to enter another file name. How can I do that?<BR><BR>&#8212; TW<\/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, TW. You know, even though we\u2019ve been writing this column for over two years now it never ceases to amaze us that people are brave enough to ask the Scripting Guys for help any time they need to do something practical. Why does that amaze us? Well, last night, for example, the Scripting Guy who writes this column decided it was time to bake something. The recipe he opted to use called for one egg white. Before going to the store for the rest of the ingredients he checked to see if the Scripting Family had any eggs; they had one. But that\u2019s all he needed, right?<\/P>\n<P>After returning from the store our hero began mixing the batter. Being as good a chef as he is a scripter, he broke his lone egg in half and, standing over the sink, carefully poured the egg from one eggshell to the other, meticulously separating the white from the yolk. When he was down, the egg had been perfectly separated: the yolk was left in the shell, and the egg white \u2013 the part he needed \u2013 had been washed down the drain. Gee; how \u2026 amusing \u2026.<\/P>\n<TABLE id=\"E3C\" 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>. Fortunately, the Scripting Guy who writes this column is used to doing things like that; in fact, he finds himself running back to the store so often that they\u2019ve given him his very own parking place.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>What that all boils down to is this: if you\u2019d rather withdraw your question, TW, and ask someone a bit smarter, well, we understand; no hard feelings. If you\u2019re willing to take a chance, however, here\u2019s a script that (we hope) will do the trick:<\/P><PRE class=\"codeSample\">Do Until i = 1\n    strFile = InputBox(&#8220;Please enter the file name: &#8220;)<\/p>\n<p>    If strFile = &#8220;&#8221; Then\n        Exit Do\n    End If<\/p>\n<p>    strPath = &#8220;C:\\Scripts\\&#8221; &amp; strFile<\/p>\n<p>    Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\n    Set objWorkbook = objExcel.Workbooks.Open(strPath)\n    Set objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>    Wscript.Echo objWorksheet.Range(&#8220;Status&#8221;)\n    objExcel.Quit\nLoop\n<\/PRE>\n<P>Let\u2019s see if we can figure out how this baby works. As you can see, we start out by creating a Do loop designed to loop until the variable <I>i<\/I> is equal to 1. This is how we\u2019re able to re-prompt the user over again: because the variable <I>i<\/I> will <I>never<\/I> be equal to 1 this loop will cause the script to run either for forever or until the user clicks <B>Cancel<\/B> in the InputBox or clicks <B>OK<\/B> without entering a file name, whichever comes first. <\/P>\n<P>Speaking of the InputBox, the very first thing we do inside the Do loop is use the <B>InputBox<\/B> method to prompt the user to enter a file name:<\/P><PRE class=\"codeSample\">strFile = InputBox(&#8220;Please enter the file name: &#8220;)\n<\/PRE>\n<P>The InputBox itself stays onscreen until the user clicks <B>OK<\/B> or <B>Cancel<\/B>; when that happens the results are stored in a variable named strFile. If the user clicks <B>OK<\/B> strFile will contain the file name entered into the InputBox; if the user clicks <B>Cancel<\/B> strFile will be equal to an empty string. With that in mind our next task is to check the value of strFile. If strFile is equal to nothing (\u201c\u201d\u201d) we call the <B>Exit Do<\/B> statement and exit the loop:<\/P><PRE class=\"codeSample\">If strFile = &#8220;&#8221; Then\n    Exit Do\nEnd If\n<\/PRE>\n<P>But what if strFile <I>isn\u2019t<\/I> equal to nothing? In that case we append the file name (that is, the value of strFile) to the string <B>C:\\Scripts\\<\/B>; this enables us to construct a full path to the file we want to open. (For example, if the user types <B>9365.xls<\/B> into the InputBox the path we construct will end up being <B>C:\\Scripts\\9365.xls<\/B>.) We then use this block of code to create an instance of the <B>Excel.Application<\/B> object, open the file whose path has been stored in strPath, and then bind to the first worksheet in the workbook:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nSet objWorkbook = objExcel.Workbooks.Open(strPath)\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>With the worksheet open our next task is to echo back the desired value. In this script, we\u2019re assuming you\u2019ve created a one-cell range named <I>Status<\/I> which contains the status information of interest. If that\u2019s the case you can echo back the status information using code like this:<\/P><PRE class=\"codeSample\">Wscript.Echo objWorksheet.Range(&#8220;Status&#8221;)\n<\/PRE>\n<P>Alternatively, you can simply specify the address of the cell containing the status information. For example, if status is kept in cell A10 (that is, row 10, column 1) you can use this code to echo back the value of cell A10:<\/P><PRE class=\"codeSample\">Wscript.Echo objWorksheet.Cells(10,1).Value\n<\/PRE>\n<P>We then close Excel, loop around, and repeat the process. And no, Excel never <I>does<\/I> appear onscreen; instead, it runs invisibly in the background. That\u2019s because we never set the <B>Visible<\/B> property to True.<\/P>\n<P>That should get you started, TW. Ideally, you\u2019d include some error-checking in here; for example, the script is doomed to fail if the file entered by the user doesn\u2019t actually exist. Even better, you might consider popping up a <B>File Open<\/B> dialog box and allowing users to select a file without having to type in a file name. We won\u2019t discuss this modified script today; for more information on adding a <B>File Open<\/B> dialog box to a script see our <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/jan05\/hey0128.mspx\"><B>previous column<\/B><\/A> on this very subject. As long as you\u2019re running on Windows XP, however, you can accomplish the same feat using <I>this<\/I> script:<\/P><PRE class=\"codeSample\">Do Until i = 1\n    Set objDialog = CreateObject(&#8220;UserAccounts.CommonDialog&#8221;)\n    objDialog.Filter = &#8220;Excel Spreadsheets|*.xls&#8221;\n    objDialog.FilterIndex = 1\n    objDialog.InitialDir = &#8220;C:\\Scripts&#8221;\n    intResult = objDialog.ShowOpen<\/p>\n<p>    If intResult = 0 Then\n        Exit Do\n    Else\n        strPath = objDialog.FileName\n    End If<\/p>\n<p>    Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\n    Set objWorkbook = objExcel.Workbooks.Open(strPath)\n    Set objWorksheet = objWorkbook.Worksheets(1)\n    Wscript.Echo objWorksheet.Range(&#8220;Status&#8221;)\n    objExcel.Quit\nLoop\n<\/PRE>\n<P>Incidentally, if anyone out there needs an egg yolk just let us know; we \u2013 alas \u2013 happen to have one we aren\u2019t using.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have approximately 100 Excel files (all with names like 9365.xls) in a folder. I\u2019d like to prompt a user for a file name, open the file, retrieve information from a specified cell, then close the file (ideally Excel would never appear on screen). I\u2019d then like to prompt the user to [&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":[48,49,22,3,4,5],"class_list":["post-66183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-retrieving-input","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have approximately 100 Excel files (all with names like 9365.xls) in a folder. I\u2019d like to prompt a user for a file name, open the file, retrieve information from a specified cell, then close the file (ideally Excel would never appear on screen). I\u2019d then like to prompt the user to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66183","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=66183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66183\/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=66183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}