Modify outlook Columns in explorer

Add-in Express™ Support Service
That's what is more important than anything else

Modify outlook Columns in explorer
 
L Thrower




Posts: 21
Joined: 2016-07-17
Is it possible to programmatically change the order of the columns showing in the outlook message explorer?
I have been able to create the custom column (see code below) but it does not display without manually changing the order or number of lines.
User added an image




        private void CreateColumnInExplorer(string sPropertyName)
        {
            Outlook.TableView tableView = null;
            Outlook.ViewField viewField = null;
            Outlook.ColumnFormat columnFormat = null;
            Outlook.NameSpace ns = null;
            Outlook.MAPIFolder folder = null;
            Outlook.Explorer currentExplorer = null;
            try
            {
                currentExplorer = OutlookApp.ActiveExplorer();
                folder = OutlookApp.ActiveExplorer().CurrentFolder;
                if (folder != null && folder.CurrentView.ViewType == Outlook.OlViewType.olTableView)
                {
                    tableView = folder.CurrentView as Outlook.TableView;
                    try
                    {
                        tableView.ViewFields.Remove(sPropertyName);
                    }
                    catch { }//Do nothing
                    viewField = tableView.ViewFields.Add(sPropertyName);
                    columnFormat = viewField.ColumnFormat;
                    columnFormat.Label = "";
                    columnFormat.Align = Outlook.OlAlign.olAlignLeft;
                    tableView.Save();
                    tableView.Apply();
                }
            }
            finally
            {
                if (tableView != null)
                    Marshal.ReleaseComObject(tableView);
                if (columnFormat != null)
                    Marshal.ReleaseComObject(columnFormat);
                if (viewField != null)
                    Marshal.ReleaseComObject(viewField);
                if (folder != null)
                    Marshal.ReleaseComObject(folder);
                if (ns != null)
                    Marshal.ReleaseComObject(ns);
                if (currentExplorer != null)
                    Marshal.ReleaseComObject(currentExplorer);
            }
        }
Posted 26 Mar, 2020 12:54:57 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello L,

Use View.XML; see https://docs.microsoft.com/en-us/office/vba/api/outlook.view.xml. I suppose you can compare the XMLs before and after you move a column in the Outlook UI and use that info to write code doing the same programmatically.


Andrei Smolin
Add-in Express Team Leader
Posted 27 Mar, 2020 01:51:50 Top
L Thrower




Posts: 21
Joined: 2016-07-17
Thank you for pointing me in the right direction. I created the following which changes the current view to a predetermined view.xml. Maybe this will help someone else in the future.

        private void SetOutlookView(Outlook.Explorer CurrentExplorer)
        {
            Outlook.View CurrentView = null;
            if (CurrentExplorer != null)
            {
                CurrentView = CurrentExplorer.CurrentView as Outlook.View;
                if (CurrentView != null)
                {
                    try
                    {
                        CurrentView.XML = IntellidoxView();
                        CurrentView.Apply();
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(CurrentView);
                    }
                }
            }

        }


IntellidoxView() is returning the XML of the desired layout.

        public string IntellidoxView()
        {
            return "<?xml version="1.0"?><view type="table">" +
            "  <viewname>Intellidox Custom View</viewname>" +
            "  <viewstyle>table-layout:fixed;width:100%;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:8pt;color:Black;font-charset:0</viewstyle>" +
            "  <viewtime>220496527</viewtime>" +
            "  <linecolor>8421504</linecolor>" +
.............................
....................................................
Posted 27 Mar, 2020 12:40:40 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Thank you for sharing this with forum readers!


Andrei Smolin
Add-in Express Team Leader
Posted 30 Mar, 2020 02:17:34 Top