Thursday, 13 September 2012

Displaying HTML body in the Appointment Item VSTO

Hi,

During the development of outlook plugin, a scenario came to render the HTML page in appointment body.
Displaying an HTML body in the mail body is easy as mail item has a property called .HTMLBody but how to do with an appointment item.
I have struggled a lot to achieve this at last achieved it :-). In order to display an html content to the appointment window I have used the below process

//Place the below in using namespace list of program using Word = Microsoft.Office.Interop.Word;


private AppointmentItem _appItem; 
private MailItem _mailItem;

 private void DisplayHTMLinAppointmentItem()
{
 _appItem.Body = <B>HTML TEST</B>


 _mailItem = this.Application.CreateItem(OlItemType.olMailItem);
 _mailItem.HTMLBody = appItem.Body;
 _mailItem.Open += new ItemEvents_10_OpenEventHandler(mailItemCopy_Open);
 _mailItem.Display();


  Inspector insp = appItem.GetInspector;
  Word.Document wordDoc = insp.WordEditor as Word.Document;
  wordDoc.Windows[1].Document.Content.Paste();
}


 private void mailItemCopy_Open(ref bool Cancel)
{
            Inspector inspMail = _mailItem.GetInspector;
            Word.Document wordDocMail = inspMail.WordEditor as Word.Document;
            wordDocMail.Windows[1].Document.Content.Copy();
            Cancel = true;
}

Above code will help us to achieve the display of HTML content in appointment item.


9 comments:

  1. hi..
    i am having a problem in displaying the html content as body of the appointment item. The content is getting displayed but data is getting displayed on a white color back ground on each line.

    No problems if the appointment is sent to outlook mail but problem is with the personal email ids of gmail,yahoo etc.,

    Any help in this case.

    Thanks,
    Anish

    ReplyDelete
    Replies
    1. Hi Anish, sorry for the delayed reply. I too didn't find the solution for this problem. Even i too have faced the same issue and the hyperlinks wont work in gamil,yahoo it displays as a plain text. If you found anything please reply me :-)

      Delete
  2. Worked well, saved my day. Thanks!!!

    I have two questions.

    1. I am able to set the HTML into the appointment. But when i opened that appointment I prompted to save the appointment which is not expected.
    2. I am not able to read the HTML content set to an appointment. Do you know how to get the data in HTML format?

    Any help on this is much appreciated....

    Regards,
    Jayashree K R

    ReplyDelete
    Replies
    1. Hi Jayashree,

      1) I haven't faced this situation. For me it got opened nicely. Sorry unable to help on this.
      2) try the below code
      Inspector inspector = this.Application.ActiveInspector();
      Word.Document doc = null;
      string htmlBody = string.Empty;
      doc = inspector.WordEditor as Word.Document;
      doc.Content.WholeStory();
      doc.Content.Copy();
      IDataObject data = Clipboard.GetDataObject();
      htmlBody = Convert.ToString(data.GetData(DataFormats.Html));

      Please let me know if it has worked or not.

      Delete
    2. Actually i did this little differently. Below is the code that I used.

      static void Appointment_Open(ref bool Cancel)
      {
      Outlook.Inspector inspector = _appointment.GetInspector;
      Word.Document wordDoc = inspector.WordEditor;
      Word.Selection selection = wordDoc.Windows[1].Selection;
      selection.WholeStory();
      selection.Copy();
      string htmlString = Clipboard.GetText(TextDataFormat.Html);

      }

      I am able to get the Html content, I have to do this on open event as the appointment will not be the active inspector.

      But first issue is still ON. I am not able to get rid of that.

      Regards,
      Jayashree K R

      Delete
    3. On issue 1, I just want to clarify one point. I am able to open the appointment correctly, but when I close that without any change I prompted for save. If I am not saving the appointment, the HTML format will go OFF and will show all html tags when it gets opened next time.

      Delete
    4. I think its the MS Outlook behaviour, if we open a new mail or meeting request and if we want to close it. Then outlook will prompt know, 'Do you want to save it to Drafts". If you want to open that appointment again, then you have to save the appointment know otherwise how you can see the same appointment item.
      Please excuse me If I'am unable to understand your point.

      Delete
  3. Meetings / Appointments use RTF . . . not HTML.

    ReplyDelete