{"id":69933,"date":"2005-04-27T21:51:00","date_gmt":"2005-04-27T21:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/27\/how-can-i-tell-if-an-excel-workbook-is-open-and-if-it-isnt-open-it\/"},"modified":"2005-04-27T21:51:00","modified_gmt":"2005-04-27T21:51:00","slug":"how-can-i-tell-if-an-excel-workbook-is-open-and-if-it-isnt-open-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-an-excel-workbook-is-open-and-if-it-isnt-open-it\/","title":{"rendered":"How Can I Tell if an Excel Workbook is Open and, If It Isn\u2019t, Open It?"},"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! How can I tell if a specific Excel workbook is open and, if it isn\u2019t, open it?<BR><BR>&#8212; RK<\/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, RK. Every now and then we see a question and think, \u201cWe should answer this one; it\u2019s so easy it won\u2019t take more than a few minutes to finish the column and then we can all take a nice long lunch.\u201d At some point you\u2019d think we\u2019d learn that every time we say that it turns out that the problem isn\u2019t as easy as it first looked, and that the column takes far more than just a few minutes to complete. But, no, we never <I>do<\/I> learn, which is why we\u2019re still hard at work trying to answer this question.<\/P>\n<P>Of course, we still plan on taking a nice long lunch, but that\u2019s another story.<\/P>\n<P>How did we get fooled on this one? Well, we knew Excel has a <B>Workbooks<\/B> collection that returns a list of all the open workbooks. Foolishly, we assumed this meant <I>all<\/I> the open workbooks. (And, yes, we know what people say about assuming things.)<\/P>\n<P>Unfortunately, though, the Workbooks collection applies only to a specific instance of Excel; that meant we couldn\u2019t just create a new instance of Excel and get back a list of <I>all<\/I> open workbooks. Instead, we\u2019d have to bind to each running instance of Excel, query its Workbooks collection, and then combine all those collections into a single list. That\u2019s theoretically possible, but\u2026.<\/P>\n<P>And so we thought to ourselves, \u201cOK, we\u2019re having difficulty solving a problem with Microsoft Excel. What do most people do when they\u2019re having problems with Excel?\u201d And once we put it that way, the answer was obvious: we\u2019d use Microsoft Word to extricate ourselves from our predicament.<\/P>\n<P>Yes, Microsoft Word. As it turns out, Word has something called the <B>Tasks<\/B> collection that can replicate many of the functions of the Windows Task Manager. (If you\u2019re interested in learning more about the Tasks collection, take a look at the <A href=\"http:\/\/null\/technet\/scriptcenter\/topics\/office\/tasks.mspx\"><B>\u201cBuild Your Own Task Manager Using Microsoft Word\u201d<\/B><\/A> article on the <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/office.mspx\"><B>Scripting for Microsoft Office<\/B><\/A> center.) As you can see here, open workbooks are displayed &#8211; by file name &#8211; in Task Manager:<\/P><IMG border=\"0\" alt=\"Task Manager\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/exceltm.jpg\" width=\"375\" height=\"434\"> \n<P><BR>We figured we could use the Tasks collection in Word to check and see if a particular workbook was open and, if not, open it. All of which gave rise to one of the craziest workarounds the Scripting Guys have ever come up with:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>Set objWord = CreateObject(&#8220;Word.Application&#8221;)\nSet colTasks = objWord.Tasks\ni = 0<\/p>\n<p>For Each objTask in colTasks\n    strName = LCase(objTask.Name)\n    If Instr(strName, &#8220;inventory.xls&#8221;) Then\n        i = 1\n    End If\nNext<\/p>\n<p>strCmdLine = &#8220;excel.exe &#8221; &amp; chr(34) &amp; &#8220;C:\\Scripts\\Inventory.xls&#8221; &amp; chr(34)<\/p>\n<p>If i = 0 Then\n    objShell.Run strCmdLine, 3\nEnd If<\/p>\n<p>objWord.Quit\n<\/PRE>\n<P>The script begins innocuously enough; we simply create an instance of the WSH <B>Shell<\/B> object (we\u2019ll use that, if we need to, to open the workbook in question). We then create an instance of the <B>Word.Application<\/B> object, and use the <B>Tasks<\/B> property to retrieve the collection of tasks running on the computer. (Tasks include all the items you see on the <B>Applications<\/B> tab in Task Manager.) We also set a variable named i to 0, for reasons we\u2019ll explain in a second.<\/P>\n<P>Next we loop through the collection of tasks. We grab a task and store the lowercase version of the task name in a variable named strName; that\u2019s what we do here: <\/P><PRE class=\"codeSample\">strName = LCase(objTask.Name)\n<\/PRE>\n<P>We then use VBScript\u2019s <B>InStr<\/B> function to see if the string <I>inventory.xls<\/I> can be found anywhere in the task name. If it is, that means Inventory.xls is already open and we set the value of i to 1. If it isn\u2019t, then Inventory.xls must not be open, and the value of i stays at 0. We then loop around and continue with all the remaining tasks.<\/P>\n<P>When we exit the loop we check the value of i. If i is equal to 0 we do two things. First we create a command string that calls Excel.exe, passing as a command-line parameter the path to Inventory.xls. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strCmdLine = &#8220;excel.exe &#8221; &amp; chr(34) &amp; &#8220;C:\\Scripts\\Inventory.xls&#8221; &amp; chr(34)\n<\/PRE>\n<P>Second, we use the <B>Run<\/B> method to call this command string. In addition to the command string we pass Run one additional parameter: the value <B>3<\/B>, which means \u201cOpen this file in a maximized window, and then make it the active window.\u201d We then call the <B>Quit<\/B> method to dismiss Microsoft Word (which, incidentally, never appears on screen) and then go about our business.<\/P>\n<P>And, yes, we\u2019re well aware of how crazy this all sounds, but it will work. Admittedly, it\u2019s not 100% foolproof; that\u2019s because only the file names are captured by the Tasks collection, not the complete file paths. That could be a problem if you have the file D:\\Archive\\Old Inventory Files\\January 2004\\Inventory.xls open; the script will see only the file name &#8211; Inventory.xls &#8211; and assume that C:\\Scripts\\Inventory.xls is open. We think there\u2019s a way to deal with that issue as well, but that will have to wait for another day.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if a specific Excel workbook is open and, if it isn\u2019t, open it?&#8212; RK Hey, RK. Every now and then we see a question and think, \u201cWe should answer this one; it\u2019s so easy it won\u2019t take more than a few minutes to finish the column and then [&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,3,5,395,714],"class_list":["post-69933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript","tag-word-application","tag-wshshell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I tell if a specific Excel workbook is open and, if it isn\u2019t, open it?&#8212; RK Hey, RK. Every now and then we see a question and think, \u201cWe should answer this one; it\u2019s so easy it won\u2019t take more than a few minutes to finish the column and then [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69933","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=69933"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69933\/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=69933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}