Outlook Email Custom Columns

This script add-in establishes custom columns for Outlook .msg email files. The most often used email fields are provided. The script requires a working Outlook application on the system.

function OnInit(initData) {
    initData.name = 'OutlookMail';
    initData.version = '2021-12-01';
    initData.copyright = '';
    initData.url = 'https://resource.dopus.com/t/outlook-email-custom-columns/39961';
    initData.desc = 'Turn Email fields into columns';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddColumns(addColData) {
    var col = addColData.AddColumn();
    col.name = 'BCC';
    col.label = 'BCC';
    col.header = 'BCC';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'Body';
    col.label = 'Body';
    col.header = 'Body';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'CC';
    col.label = 'CC';
    col.header = 'CC';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'SenderEmailAddress';
    col.label = 'SenderEmailAddress';
    col.header = 'SenderEmailAddress';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'SenderName';
    col.label = 'SenderName';
    col.header = 'SenderName';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'SentOn';
    col.label = 'SentOn';
    col.header = 'SentOn';
    col.justify = 'right';
    col.type = 'datetime';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'Size';
    col.label = 'Size';
    col.header = 'Size';
    col.justify = 'left';
    col.type = 'number';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'Subject';
    col.label = 'Subject';
    col.header = 'Subject';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';

    var col = addColData.AddColumn();
    col.name = 'To';
    col.label = 'To';
    col.header = 'To';
    col.justify = 'left';
    col.multicol = true;
    col.autogroup = true;
    col.method = 'OnColumn';
}

var outlookApp = new ActiveXObject('Outlook.Application');

function OnColumn(scriptColData) {
    var item = scriptColData.item;
    if (item.is_dir) return;
    if (item.ext != '.msg') return;

    if (outlookApp == null) return;

    var mailItem = outlookApp.CreateItemFromTemplate(item);

    scriptColData.columns('BCC').value = mailItem.BCC;
    scriptColData.columns('Body').value = mailItem.Body;
    scriptColData.columns('CC').value = mailItem.CC;
    scriptColData.columns('SenderEmailAddress').value = mailItem.SenderEmailAddress;
    scriptColData.columns('SenderName').value = mailItem.SenderName;
    scriptColData.columns('SentOn').value = mailItem.SentOn;
    scriptColData.columns('Size').value = mailItem.Size;
    scriptColData.columns('Subject').value = mailItem.Subject;
    scriptColData.columns('To').value = mailItem.To;

    mailItem.Close(1); // 1 = Changes to the document are discarded.

    // outlookApp.Quit();
}

How to install

How to use

  • Select columns via Script/OutlookMail

or

  • Toggle columns via a button
Set COLUMNSTOGGLE="scp:OutlookMail/SentOn(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/SenderName(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/SenderEmailAddress(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/To(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/CC(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/BCC(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/Subject(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/Size(!,a,0)"
Set COLUMNSTOGGLE="scp:OutlookMail/Body(!,a,0)"
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Toggle Outlook</label>
	<icon1>#usercommand</icon1>
	<icon2>#email</icon2>
	<function type="normal">
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/SentOn(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/SenderName(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/SenderEmailAddress(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/To(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/CC(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/BCC(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/Subject(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/Size(!,a,0)&quot;</instruction>
		<instruction>Set COLUMNSTOGGLE=&quot;scp:OutlookMail/Body(!,a,0)&quot;</instruction>
	</function>
</button>

Like all Opus columns, these columns can also be used for renaming. The SentOn column can be formatted as a date.

Need more fields? All available email fields can be found here:
https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem
MailItem object (Outlook) > Properties

Changelog:

  • 2021-12-01: Script shouldn't end the main Outlook.

How to use buttons and scripts from this forum

4 Likes