title: Determine currently active Outlook store in vba / wsh vbs
body: <p>I'm using JavaScript and the Windows Scripting Host to work with Outlook items. </p>
  
  <p>This has been straightforward enough, until one of our users pointed out that the script does not work because he has two Exchange profiles configured in Outlook. </p>
  
  <p>The one the script should be working on is in the second profile, but if I access, e.g. the Outlook categories master list, this is loaded from the standard profile.</p>
  
  <p>The answer is easy enough, I just need to address the correct <a href="https://msdn.microsoft.com/en-us/library/office/ff862128.aspx" rel="nofollow noreferrer">Outlook store object</a>. My script needs to determine, which store is being accessed in the currently active <a href="https://msdn.microsoft.com/en-us/library/office/ff860356.aspx" rel="nofollow noreferrer">Outlook Explorer</a>, and I could not find a native function for this.</p>
  
  <p>Given an <a href="https://msdn.microsoft.com/EN-US/library/office/ff866895.aspx" rel="nofollow noreferrer">outlook application object</a> <code>var ol = new ActiveXObject( "Outlook.Application" )</code>, I can natively determine the currently active Outlook explorer by simply calling <code>ol.[ActiveExplorer()][4];</code> But the session object, through which stores are accessed, does not offer an equivalent function.</p>
  
  <p>I've come up with a workaround, by comparing the <a href="https://msdn.microsoft.com/en-us/library/office/ff866079.aspx" rel="nofollow noreferrer">explorer's caption</a></p>
  
  <pre><code>ol.ActiveExplorer().Caption // "Inbox - email@address.com - Outlook"
  </code></pre>
  
  <p>with a loop over the stores' <a href="https://msdn.microsoft.com/en-us/library/office/ff866767.aspx" rel="nofollow noreferrer">display names</a></p>
  
  <pre><code>ol.Session.Stores.Item(counter).DisplayName // "email@address.com"
  </code></pre>
  
  <p>So I do have working code:</p>
  
  <pre><code>function ActiveStore(olApplication) {
      var ActiveExplorerCaption=olApplication.ActiveExplorer().Caption;
      for (var storeCounter=1; storeCounter&lt;=olApplication.Session.Stores.Count; storeCounter++) {
          var storeDisplayName=olApplication.Session.Stores.Item(storeCounter).DisplayName;
          if (ActiveExplorerCaption.indexOf(storeDisplayName)!=-1) return olApplication.Session.Stores.Item(storeCounter);
      }
      return "undefined";
  };
  
  var ol = new ActiveXObject( "Outlook.Application" );
  WScript.Echo("The Outlook store currently active in an explorer is called "+ActiveStore(ol).DisplayName);
  </code></pre>
  
  <p>But, I'm wondering:</p>
  
  <ul>
  <li>will this code always work, i.e. are this variables always set up in this way, or just in the installations I can work with</li>
  <li>isn't there a better way to determine the currently active store?</li>
  </ul>