Search code examples
vbavbscriptoutlookwsh

Determine currently active Outlook store in vba / wsh vbs


I'm using JavaScript and the Windows Scripting Host to work with Outlook items.

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.

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.

The answer is easy enough, I just need to address the correct Outlook store object. My script needs to determine, which store is being accessed in the currently active Outlook Explorer, and I could not find a native function for this.

Given an outlook application object var ol = new ActiveXObject( "Outlook.Application" ), I can natively determine the currently active Outlook explorer by simply calling ol.[ActiveExplorer()][4]; But the session object, through which stores are accessed, does not offer an equivalent function.

I've come up with a workaround, by comparing the explorer's caption

ol.ActiveExplorer().Caption // "Inbox - [email protected] - Outlook"

with a loop over the stores' display names

ol.Session.Stores.Item(counter).DisplayName // "[email protected]"

So I do have working code:

function ActiveStore(olApplication) {
    var ActiveExplorerCaption=olApplication.ActiveExplorer().Caption;
    for (var storeCounter=1; storeCounter<=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);

But, I'm wondering:

  • 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
  • isn't there a better way to determine the currently active store?

Solution

  • Since you have ActiveExplorer, you also have Explorer.CurrentFolder and thus Folder.StoreID. Then use Namespace.GetStoreFromID using that StoreID value.