I'm working on a plugin for Outlook. I need to always have an up-to-date list of Outlook accounts (File->Add Account), but when I take this data from NameSpace.Accounts, they are relevant only at the time Outlook starts. If I add another account or delete the old one, these changes will not be reflected in NameSpace.Accounts. Is there any way to refresh this data?
I am using the following code to get accounts
var outlookObj = new Outlook.Application();
var accounts = outlookObj.Application.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
//do something
}
CodePudding user response:
Firstly, do not use new Outlook.Application()
in an addin - an instance of the Application
object is provided to your addin on startup, and it is not handicapped by the security prompts..
Also try to avoid caching/keeping a reference to the Accounts
or Account
object.
If using Redemption is an option (I am its author), it exposes RDOAccounts collection (which is never cached) and, unlike the Accounts collection in OOM, also exposes events when accounts are added/deleted/modified/rearranged.
CodePudding user response:
The Outlook object model doesn't provide such events for accounts. But you may try to handle the Stores.StoreAdd event which is fired when a Store
has been added to the current session either programmatically or through user action.
Note, as a possible workaround you may consider using a third-party library such as Redemption
which can monitor changes to Accounts in Outlook.
There is no need to create a new Application instance in the code:
var outlookObj = new Outlook.Application();
Instead, in VSTO based add-ins you need to use the ThisAddin
class where the Application
property is provided out of the box.
CodePudding user response:
Ok, thanks. But how can I add additional subfolders programmatically to default folder (calendar or contacts type) in new accounts without having an "Account" object? What matters to me is the actual changes to the accounts to know the account's DeliveryStore so that I can create child folders and deliver calendar data or contacts there.
I create subfolders like this:
var folderType = OlDefaultFolders.olFolderCalendar;
var defaultFolder = outlookAccount.Session.GetDefaultFolder(folderType);
defaultFolder.Folders.Add(folderName,
folderType);
Can i do it without known about added new Account entity?