RTF to HTML format change

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

RTF to HTML format change
Setting the body format of a mail item, causes format to change 
Jacob Vestergaard


Guest


Dear ADX,

We are having trouble setting the body format of a MailItem without altering the mail itself. To reproduce our issue, try:

1) Hit Reply to an RTF (or plain text) mail.
2) Push your home-made button on the Ribbon, where in the Click-event the approach described http://www.add-in-express.com/creating-addins-blog/2011/09/19/outlook-email-formats-programmatically/ is used to set the BodyFormat to HTML.
3) Observe that (1) the format of the text changes (probably inevitable?) but also (2) that the reply-header disappears from the mail. By "reply-header" I mean the text-part before the mail being replied to,

From: ds@adsada.com
Sent: somedate
To: Someone
Subject: My subject

However, if I - instead of modifying by setting the BodyFormat by code - manually choose the Format Text tab in the ribbon and sel ect HTML as format, nothing disappears, formats do not change, etc. So clearly Outlook itself has a good idea how this conversion should be done, which is not achieved by the blog-example.

So my question is: How do I properly change fr om RTF (or plain text) to HTML?

Note: I use Outlook 2010 and ADX 7.0.4023. (I think at least outlook 2003 and outlook 2007 behave the same way)

Best regards,
Jacob S. Vestergaard
Posted 02 Nov, 2012 06:58:01 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Jacob,

Thank you for the detailed description.

I would suggest asking Outlook to do something it can do better than its Object Model. Please have a look at the code below:

private void HTMLButton_OnClick(object sender, IRibbonControl control, bool pressed)
{
    this.RibbonControlExecute("MessageFormatHtml");
}

private void PlainTextButton_OnClick(object sender, IRibbonControl control, bool pressed)
{
    this.RibbonControlExecute("MessageFormatPlainText");
}

private void RTFButton_OnClick(object sender, IRibbonControl control, bool pressed)
{
    this.RibbonControlExecute("MessageFormatRichText");
}


The idea is to hit Outlook's standard buttons that change the format of an email being composed. I have just tested the above code in Outlook 2013, it works well.
Posted 02 Nov, 2012 08:34:04 Top
Jacob Vestergaard


Guest


Thank you! I was completely unaware of the RibbonControlExecute. Exactly what I was looking for!

I assume that this does not work in Outlook 2003? Or is there a similar approach for that version?
Posted 02 Nov, 2012 08:43:18 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Jacob,

In Outlook 2003 you need to use another but similar approach - find a command bar control (button) on the Outlook Inspector's main menu and execute it. Please see the sample code below:

        private void CommandBarButtonPlain_Click(object sender)
        {
            // Plain Text == 5563
            Office._CommandBarButton btn = FindCommandBarButton(5563);
            if (btn != null)
                try
                {
                    if (btn.Enabled)
                        btn.Execute();
                }
                finally { Marshal.ReleaseComObject(btn); }
        }

        private void CommandBarButtonHtml_Click(object sender)
        {
            // Html == 5564
            Office._CommandBarButton btn = FindCommandBarButton(5564);
            if (btn != null)
                try
                {
                    if (btn.Enabled)
                        btn.Execute();
                }
                finally { Marshal.ReleaseComObject(btn); }
        }

        private void CommandBarButtonRtf_Click(object sender)
        {
            // Rich Text == 5565
            Office._CommandBarButton btn = FindCommandBarButton(5565);
            if (btn != null)
                try
                {
                    if (btn.Enabled)
                        btn.Execute();
                }
                finally { Marshal.ReleaseComObject(btn); }
        }

        private Office._CommandBarButton FindCommandBarButton(int id)
        {
            Outlook._Inspector inspector = OutlookApp.ActiveInspector();
            if (inspector != null)
                try
                {
                    Office._CommandBars cmdBars = inspector.CommandBars;
                    if (cmdBars != null)
                        try
                        {
                            return cmdBars.FindControl(Office.MsoControlType.msoControlButton, id, Type.Missing, Type.Missing) as Office._CommandBarButton;
                        }
                        finally { Marshal.ReleaseComObject(cmdBars); }
                }
                finally { Marshal.ReleaseComObject(inspector); }
            return null;
        }
Posted 02 Nov, 2012 10:34:06 Top
Jacob Vestergaard


Guest


That was perfect! Wish I'd asked you guys earlier, could've saved me some hours:)

Thanks!
Posted 04 Nov, 2012 10:43:08 Top