How to preview msg files using preview handler

Answered How to preview msg files using preview handler

  • Tuesday, July 20, 2010 3:13 AM
     
      Has Code

    I want to preview outlook files(*.msg) in my own program. When I create instance using '435fdba0-964c-43a7-8aff-cc94e21b2249'. It always prompt a warning dialog and tell me  "Either there is no default mail client or the current mail client cannot fulfill the messaging request. Please run Microsoft Outlook and set it as the default mail client". I use the same way to preview word ,excel and ppt files. All of them are OK. Only msg files cannot be previewed. Do you know the reason? Or anyother ways to preview msg files. The followings are some codes I used.

     

    string CLSID = "435fdba0-964c-43a7-8aff-cc94e21b2249";
    
                Guid g = new Guid(CLSID);
    
                Type a = Type.GetTypeFromCLSID(g, true);
    
                object obj = Activator.CreateInstance(a);
    
    
                IInitializeWithFile fileInit = obj as IInitializeWithFile;
                IInitializeWithStream streamInit = obj as IInitializeWithStream;
                bool isInitialized = false;
                if (fileInit != null)
                {
                  fileInit.Initialize(fileName, 0);
                  isInitialized = true;
                }
                else if (streamInit != null)
                {
                  COMStream stream = new COMStream(File.Open(fileName, FileMode.Open));
                  streamInit.Initialize((IStream)streamInit, 0);
                  isInitialized = true;
                }
    
    Weipeng

All Replies

  • Tuesday, July 20, 2010 4:12 AM
    Moderator
     
     
    .msg files are an Outlook-specific file format.  If Outlook is not installed and/or it is not the default application handler for that file type, your code will fail to open the file.
    (P.S. Please don't forget to mark this post as an answer if it has helped you)
    Eric Legault: MVP (Outlook), MCTS (SharePoint)
    Owner: Collaborative Innovations
    Twitter: elegault
    Blog: Eric Legault My Eggo
  • Tuesday, July 20, 2010 1:42 PM
     
     Answered

    If Outlook is installed, make sure the bitness of your app matches that of Outlook.

    In case of Outlook 2010 64 bit, your app must be 64 bit.

    In case of all other versions of Outlook, your app must be 32 bit no matter what the bitnes of the OS is. Make sure youe app is compiled as x86 rather than "Any CPU" or x64.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
  • Wednesday, July 21, 2010 5:14 AM
     
     

    Thanks for your reply.

     

    Could you give me a sample using preview handler to view .msg files?

     

    Thanks,

    Weipeng

  • Wednesday, July 21, 2010 6:03 AM
     
     

    I do not have a sample code, I was just poting out what the error means.

    Does anything mentioned by Eric or myself apply?

    What version of Outlook are you using?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
  • Thursday, July 22, 2010 1:26 AM
     
     
    Outlook 2007
  • Thursday, July 22, 2010 1:28 AM
     
     
    What is your OS version? Is it 64 bit?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
  • Thursday, July 22, 2010 1:29 AM
     
     
    yes, it is 64 bit.
  • Thursday, July 22, 2010 1:32 AM
     
     
    Did you set the compile target as x86 (32 bit) instead of 64 bit or "Any CPU"?
    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
  • Thursday, July 22, 2010 2:13 AM
     
     

    I have try to do on x86 machine. It is no the errors above, but I also cannot preview the msg file in the preview handler provided by outlook. So I don't know why? why the outlook can preview the msg file?

     

  • Thursday, July 22, 2010 2:28 PM
     
     

    How does "cannot preview" manifest itself?

    I see no code that actually displays the MSG file.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
  • Sunday, August 29, 2010 9:22 AM
     
     

    Dmitry Streblechenko,

     

    Do you see the my codes above, I mean I cannot preview msg file even if outlook 2007 has been installed. But use the same codes, change the GUID which match the word preview handler, it is ok. I don't know why?

     

     

  • Sunday, August 29, 2010 7:49 PM
     
     

    I can see code that creates an instance of a COM object and initializes it.

    Where exactly is the preview supposed to be shown? The preview object needs to be somehow hosted by another window.

    How that is done, I do nto know - I have no experience with the preview handlers. My post simply explained what the MAPI error meant.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
  • Monday, August 30, 2010 2:12 PM
     
     

    Yes, the above is core preview method. The preview object needs to be somehow hosted by another window.