{"id":11342,"date":"2021-12-08T02:39:28","date_gmt":"2021-12-08T02:39:28","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=11342"},"modified":"2021-12-08T02:39:47","modified_gmt":"2021-12-08T02:39:47","slug":"solved-run-cmd-file-from-a-command-button-in-excel","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/office\/run-cmd-file-from-a-command-button-in-excel\/16871.html","title":{"rendered":"Solved Run .Cmd File From a Command Button In Excel"},"content":{"rendered":"<p>I have a .cmd script that succesfully works directly from the DOS command line, or by simply clicking on it in Explorer. The script looks within a specific folder and randomly opens a jpg file from within it. I want to use this within the Office tools, a command button that when clicked runs the script.<\/p>\n<p>This is what i have in Excel:<\/p>\n<p>Private Sub CommandButton3_Click()<br \/>\nDim RetVal<br \/>\nRetVal = shell(&#8220;D:\\Mythos\\rand.cmd&#8221;, 1)<br \/>\nEnd Sub<\/p>\n<p>When I click on the command button, an explorer window opens. I&#8217;m expecting a jpg file to open in Office Picture Manager (or whatever jpg viewer I wish to use)<\/p>\n<p>The script looks like this:<\/p>\n<p>setlocal EnableDelayedExpansion<br \/>\nset count=0<br \/>\nfor \/f &#8220;delims=&#8221; %%a in (&#8216;dir \/b *.jpg&#8217;) do @(<br \/>\nset filename[!count!]=%%a<br \/>\nset \/a count = count + 1<br \/>\n)<br \/>\nset \/a choose = (%random% * 32768 + %random%) %% count<br \/>\nset chosen=!filename[%choose%]!<br \/>\nstart &#8220;Opening %chosen%&#8221; &#8220;%chosen%&#8221;<\/p>\n<p>Any ideas where I&#8217;m going wrong?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":9},"question-category":[53],"question_tags":[],"class_list":["post-11342","question","type-question","status-publish","hentry","question-category-office"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/11342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=11342"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=11342"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=11342"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=11342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}