Page 1 of 1

How to extract .msg file from Outlook to TrichView

Posted: Mon Oct 02, 2023 2:28 am
by tobyfleming
Hello, Can you guide me on how to extract .msg file from Outlook to Trichview? I'm looking for how to do it but without success. Thanks

Re: How to extract .msg file from Outlook to TrichView

Posted: Mon Oct 02, 2023 9:14 am
by Sergey Tkachenko
TRichView does not include code for parsing MSG files.
However, if you have Outlook installed, you can use Outlook COM object to open MSG file in TRichViewEdit.

There is a demo showing how to compose email in TRichViewEdit and send or save it using Outlook object:
https://www.trichview.com/forums/viewtopic.php?t=10734

Here is the code for opening MSG file (file choosing and error checking are skipped for simplicity). You can call this code on button click in this demo:

Code: Select all

var
   MI: _MailItem;
   Stream: TMemoryStream;
   Body: UnicodeString;
begin
   MI := App.Session.OpenSharedItem(FILENAME) as _MailItem;
   txtSubject.Text := MI.Subject;

   Stream := TMemoryStream.Create;
   Body := MI.HTMLBody;
   Stream.WriteBuffer(PWideChar(Body)^, Length(Body) * sizeof(WideChar));
   Stream.Position := 0;
   RichViewEdit1.Clear;
   RichViewEdit1.DeleteUnusedStyles(True, True, True);
   RichViewEdit1.LoadHTMLFromStream(Stream, '', 1200 {UTF-16});
   RichViewEdit1.Format;
   Stream.Free;
MI.HTMLBody always returns HTML for the email object (even if this is a plain text email; in this case, the object gets HTML from a plain text).
The code above displays email body and subject. You can also check other fields of MI (such as Recipients and Sender)