Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppressing Outlook Field Printing

Tags:

c#

outlook

We have written an add-on for Outlook that files emails into our CRM system. Int he process of this, it saves the Outlook Message ID as a UserField on the Message itself.

eg.

currentUserProperty = Constants.APPLICATION_NAME + "EntryID";
mailItem.UserProperties.Add(currentUserProperty,
       Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText,
       Missing.Value,
       Missing.Value).Value = entryId;

Unfortunately, this is a HUUUGGEE number, much like:

"00000000D502D779150E2F4580B1AADDF04ECDA6070097EF5A1237597748A4B4F9BFF540020800000006E9E4000068BB5B6DFC36924FAEC709A17D056583000002DE0E350000"

The problem is that when the user prints the message off, Outlook insists on including this field (beneath the From/To) and because it has no spaces, cannot wrap the ID and compresses the A4 page until it can fit on horizontally. This produces teeny-tiny email printouts.

Is there any way I can correct this? I had thought of overwriting the field OriginalEntryID (which is the one causing the problem) with one delimited by spaces but I get an exception from the COM layer. My next stop is to try and suppress the output of this and other user-defined fields on Outlook stationary.

Does anyone know how this can be achieved?

like image 955
Program.X Avatar asked Dec 03 '22 15:12

Program.X


1 Answers

You must use .NET Reflection to fix this (as recommended by Microsoft Support). Hopefully this will be fixed in future versions of the VSTO SDK.

Suppress Outlook User Field Printing

static void SuppressUserPropertyPrinting(Outlook.MailItem message)
{
    try
    {   // Late Binding in .NET: https://support.microsoft.com/en-us/kb/302902
        Type userPropertyType;
        long dispidMember = 107;
        long ulPropPrintable = 0x4; // removes PDO_PRINT_SAVEAS
        string dispMemberName = String.Format("[DispID={0}]", dispidMember);
        object[] dispParams;

        if (message.UserProperties.Count == 0) return; // no props found (exit)

        // marks all user properties as suppressed
        foreach (Outlook.UserProperty userProperty in message.UserProperties.Cast<Outlook.UserProperty>())
        {
            if (userProperty == null) continue; // no prop found (go to next)
            userPropertyType = userProperty.GetType(); // user property type

            // Call IDispatch::Invoke to get the current flags
            object flags = userPropertyType.InvokeMember(dispMemberName, BindingFlags.GetProperty, null, userProperty, null);
            long lFlags = long.Parse(flags.ToString()); // default is 45 - PDO_IS_CUSTOM|PDO_PRINT_SAVEAS|PDO_PRINT_SAVEAS_DEF (ref: http://msdn.microsoft.com/en-us/library/ee415114.aspx)

            // Remove the hidden property Printable flag
            lFlags &= ~ulPropPrintable; // change to 41 - // PDO_IS_CUSTOM|PDO_PRINT_SAVEAS_DEF (ref: http://msdn.microsoft.com/en-us/library/ee415114.aspx)

            // Place the new flags property into an argument array
            dispParams = new object[] { lFlags };

            // Call IDispatch::Invoke to set the current flags
            userPropertyType.InvokeMember(dispMemberName, BindingFlags.SetProperty, null, userProperty, dispParams);
        }
    }
    catch { } // safely ignore if property suppression doesn't work
}
like image 96
SliverNinja - MSFT Avatar answered Dec 19 '22 21:12

SliverNinja - MSFT