Search code examples
outlookvstooutlook-addinoffice-addinsexcel-addins

Outlook - Exporting emails to Excel - Each folder as new worksheet C#


I have following code which would export emails from each folder of shared mailbox to Excel file. Currently, it creates one Excel file for each folder but I want to change it to create one worksheet for each folder and combine all sheets into one Excel file.

            Outlook.Application application = Globals.ThisAddIn.Application;
            Outlook.NameSpace ns = application.GetNamespace("MAPI");

            Excel.Application oApp = null;
            Excel.Workbook oWB = null;
            Excel.Worksheet oSheet = null;
            oApp = new Excel.Application();
            oWB = oApp.Workbooks.Add();
            string recipientName = "xxxxxx@XXXXXXX.com";
            Outlook.Recipient recip = ns.CreateRecipient(recipientName);
            recip.Resolve();
            if (recip.Resolved)
            {
                Outlook.MAPIFolder folderContacts = ns.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Folder allFolder = folderContacts.Parent;
                    foreach (Outlook.Folder rfolder in allFolder.Folders)
                    {
                        if (rfolder.Name.Contains("In Progress"))
                        {
                            Outlook.UserProperties MailUserProperties = null;
                            Outlook.UserProperty MailUserProperty = null;

                            oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1);
                            oSheet.Name = rfolder.Name;
                            Excel.Range last = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                            DateTime startDate = new DateTime(2019, 10, 1);
                            DateTime lastDate = new DateTime(2019, 10, 5);
                            string filter = "[ReceivedTime] >= '" + startDate.ToString("g") + "' AND [ReceivedTime] <= '" + lastDate.ToString("g") + "'";
                            Outlook.Items restrictedMails = rfolder.Items.Restrict(filter);

                            int row = 2;
                            int column = 1;
                            Excel.Range range = (Excel.Range)oSheet.Cells[row, column];

                            oSheet.Cells[1, 1] = "Received Date";
                            oSheet.Cells[1, 2] = "Sender";
                            oSheet.Cells[1, 3] = "Subject";
                            oSheet.Cells[1, 4] = "Category";
                            try
                            {
                                for (int i = 1; i <= restrictedMails.Count; i++)
                                {
                                    Outlook.MailItem mail = restrictedMails[i];
                                    MailUserProperties = mail.UserProperties;
                                    range.Cells[i, 1] = mail.ReceivedTime;
                                    range.Cells[i, 2] = mail.Sender;
                                    range.Cells[i, 3] = mail.Subject;
                                    range.Cells[i, 4] = mail.Categories;
                                }
                                oSheet.UsedRange.Columns.AutoFit();
                            }

                            catch (System.Runtime.InteropServices.COMException ex)
                            {
                                Console.WriteLine(ex.ToString());
                            }
                        }
                    }

                SaveFileDialog saveFileDialogue = new SaveFileDialog();
            }

Thank you in advance.


Solution

  • You just need to use the for loop instead of foreach:

                Outlook.Application application = Globals.ThisAddIn.Application;
                Outlook.NameSpace ns = application.GetNamespace("MAPI");
    
                Excel.Application oApp = null;
                Excel.Workbook oWB = null;
                Excel.Worksheet oSheet = null;
                oApp = new Excel.Application();
                oWB = oApp.Workbooks.Add();
                string recipientName = "xxxxxx@XXXXXXX.com";
                Outlook.Recipient recip = ns.CreateRecipient(recipientName);
                recip.Resolve();
                if (recip.Resolved)
                {
                    Outlook.MAPIFolder folderContacts = ns.GetSharedDefaultFolder(recip, Outlook.OlDefaultFolders.olFolderInbox);
                    Outlook.Folder allFolder = folderContacts.Parent;
                    Outlook.Folders allFolders = allFolder.Folders;
                        for(int i =1; i<= allFolders.Count; i++)
                        {
                            Outlook.Folder rfolder = allFolders[i];
    
                            if (rfolder.Name.Contains("In Progress"))
                            {
                                Outlook.UserProperties MailUserProperties = null;
                                Outlook.UserProperty MailUserProperty = null;
    
                                oSheet = (Excel.Worksheet)oWB.Worksheets.Add();
                                ' or just check whether such worksheet exists before adding a new one
    
    
                                oSheet.Name = rfolder.Name;
                                Excel.Range last = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                                DateTime startDate = new DateTime(2019, 10, 1);
                                DateTime lastDate = new DateTime(2019, 10, 5);
                                string filter = "[ReceivedTime] >= '" + startDate.ToString("g") + "' AND [ReceivedTime] <= '" + lastDate.ToString("g") + "'";
                                Outlook.Items restrictedMails = rfolder.Items.Restrict(filter);
    
                                int row = 2;
                                int column = 1;
                                Excel.Range range = (Excel.Range)oSheet.Cells[row, column];
    
                                oSheet.Cells[1, 1] = "Received Date";
                                oSheet.Cells[1, 2] = "Sender";
                                oSheet.Cells[1, 3] = "Subject";
                                oSheet.Cells[1, 4] = "Category";
                                try
                                {
                                    for (int i = 1; i <= restrictedMails.Count; i++)
                                    {
                                        Outlook.MailItem mail = restrictedMails[i];
                                        MailUserProperties = mail.UserProperties;
                                        range.Cells[i, 1] = mail.ReceivedTime;
                                        range.Cells[i, 2] = mail.Sender;
                                        range.Cells[i, 3] = mail.Subject;
                                        range.Cells[i, 4] = mail.Categories;
                                    }
                                    oSheet.UsedRange.Columns.AutoFit();
                                }
    
                                catch (System.Runtime.InteropServices.COMException ex)
                                {
                                    Console.WriteLine(ex.ToString());
                                }
                            }
                        }
    
                    SaveFileDialog saveFileDialogue = new SaveFileDialog();
                }