in Education by
I am developing Outlook add-ins which populates all the available shared mailboxes in to combo box and sends email using the selected mailbox. When I select the mail account from combo box, I get error Unable to cast object of type 'System.String' to type 'Microsoft.Office.Interop.Outlook.Store' Following is the code. Populate combo box. private void MailBoxOptions_Load(object sender, EventArgs e) { Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace ns = application.GetNamespace("MAPI"); Stores stores = ns.Stores; foreach (var store in Globals.ThisAddIn.Application.Session.Stores .Cast() .Where(c => c.ExchangeStoreType == Microsoft.Office.Interop.Outlook.OlExchangeStoreType.olExchangeMailbox)) { if (store != null) { mailBoxes.Items.Add(store.DisplayName); } else { MessageBox.Show("You don't have access to any shared mail-inbox."); } } } Code for Combo box public void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { var selectedStore = (Store)mailBoxes.SelectedItem; } Any help would be appreciated. Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
With mailBoxes.Items.Add(store.DisplayName); you are adding the display name of the store as string to the ComboBox. And that's exacly what you get in return with mailBoxes.SelectedItem. Of course you cannot cast this string to Store. You could wrap the store in a display class public class StoreDisplay { public StoreDisplay(Store store) { this.Store = store; } public Store Store { get; } public override string ToString() ==> Store.DisplayName; } Then you can add items to the ComboBox with mailBoxes.Items.Add(new StoreDisplay(store)); Because ToString has been overridden, the ComboBox will show the DisplayName or every store item. Finally, you can retrive the store with var selectedStore = ((StoreDisplay)mailBoxes.SelectedItem)?.Store; if (selectedStore != null) { ... } You can also try to add the Store objects directly to the ComboBox; however, I don't know if they will display correctly. A side note: If you have conflicting type names or if you simply want shorter namespace references, you can use namespace aliases using MsOl = Microsoft.Office.Interop.Outlook; using AppSession = Globals.ThisAddIn.Application.Session; and use it like this var application = new MsOl.Application(); MsOl.NameSpace ns = application.GetNamespace("MAPI"); Stores stores = ns.Stores; foreach (var store in AppSession.Stores .Cast() .Where(c => c.ExchangeStoreType == MsOl.OlExchangeStoreType.olExchangeMailbox)) { ... }

Related questions

0 votes
    I am developing Outlook add-ins which populates all the available shared mailboxes in to combo box and sends ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    How do I use the CAST in ADO to convert Float values to String? I tried SELECT CAST([Field] AS ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Has anyone got any suggestions for unit testing a Managed Application Add-In for Office? I'm using NUnit but I had the ... .NET object that I want to test (all set under condition...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Has anyone got any suggestions for unit testing a Managed Application Add-In for Office? I'm using NUnit ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    3.Microsoft…………..lis a part of the Microsoft office package. Blanks Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    What are the data types supported by Microsoft Office Access 2010 Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    How to add a page in a started word document in Microsoft Office Word Select the correct answer from above options...
asked Dec 5, 2021 in Education by JackTerrance
0 votes
    How to add a page in a started word document in Microsoft Office Word Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    Which of these is a class which uses String as a key to store the value in object? (a) Array (b) ... The Collections Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these is a class which uses String as a key to store the value in object? (a) Array ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Im using EntityFramework Code First Generic Repository. I have one filter method. Also this method make paging ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Which operator is having right to left associativity in the following? W A. O array subscription B. O ... addition and substraction Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    (1) WO One of the following is not an operating system: (1) MS-windows (ii) Mac OS (iii) MS-Office (iv) none of these Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    I've created a semi-transparent test image, filled with 128-alpha black. I draw it while alternating ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I am trying to declare a list which can either be full of type 'a' data or type 'b' at a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...