Advertisement

vista and office dev: creating preview handlers

(long, informative post with images -- apologies aggregates...click on the link :-))

in outlook 2007, when you have an attachement in a mail message and click on the attachment file, you are presented with an option to preview this attachment directly in outlook...just like the preview pane for a normal message:

previewmode

likewise in vista's file explorers, you can enable a preview pane and get the same functionality for highlighting a file in explorer as seen here:

previewvista

for productivity, namely in outlook 2007 for me, this is a great advancement to the platform.  i now don't have to double-click a file, etc and launch another program to see what usually is just a glance that is required.  this preview mode in the file explorer has existing in windows xp, but was little used by developers.  this is likely due to the warnings and caution from microsoft to not use managed code for this feature.  in winxp, this functionality is executed in process, thus making it a dangerous path when multiple versions of the CLR might be invovled.

in vista, this is much more simplified and the model has changed to out of process...finally making it easier to do these things in managed code.  has an excellent article in the january edition of msdn magazine that covers this entire story and the in's and out's of preview handler development.  the great thing about his article aside from the depth of understanding you will gain is that stephen has created a managed interface to make it easier to implement your own preview handler.  you can download his code from the msdn preview handler code link here.  stephen encourages us to write our own preview handlers, but he's gone and done most of the ones i think we all use -- zip, xml, xaml, resx, csv, msi, internet explorer, etc.  they are all awesome.  the zip preview handler is especially cool since it also implements a treeview representation of the contents of the zip file...look at this sample showing the vista preview, but would be the same in outlook 2007 (note: this zip one also handles .gadget files):

zippvwhandler

one handler that stephen also provides is a pdf preview handler (probably the second one i will use the most next to the zip one).  in his sample, he uses the adobe activex control for the acrobat reader implementation.  now this would require that you have adobe reader installed.  if you are a frequent reader, you may know that i'm not a fan of adobe reader.  i use foxit reader from .  it's lean and mean.  i wanted to ensure i could still use the pdf preview handler, but had to modify a few things.

first i had to get the foxit activex sdk from their site.  there is a license cost to use this, which i'm currently investigating.  after getting the activex control, i modified stephen's code at about line 41 of of the PdfPreviewHandler.cs file in the download.  i changed it to:

// Foxit Reader ActiveX
public PdfAxHost()
    : base("d46a7492-4b6c-446f-8100-4812edf406c9") { }
 

then, i also had to change the pinvoke method.  the adobe control uses a LoadFile() signature, but foxit uses an OpenFile() signature.  foxit also uses a second parameter, the starting page of the document.  so i changed it to this code:

_ocx.GetType().InvokeMember(
  "OpenFile", BindingFlags.InvokeMethod, null, // changed to OpenFile
   _ocx, new object[] { fileName, 0 }, CultureInfo.InvariantCulture); // added 0 parameter to array
 

compiled and deployed...now i have pdf previewing in outlook 2007:

foxitpreview

it's a beautiful thing.  please check out stephen's article in msdn magazine -- it was a great read and well done -- i totally understood the concepts presented and it allowed me to create a few handlers of my own.  as a developer i frequently get code files sent as attachments.  i generally have to open them up and look at them in visual studio or notepad2 for code highlighting, etc.  well, with my new found knowledge i created a code preview handler.  it will see .cs, .vb, .js and .sql files and enable a previewer to show them in code-highlighted formatting in the preview window of outlook 2007 or windows vista explorer.  here's a sample:

codeprievewer

how is this accomplished?  well, aside from the great library stephen provided, i pulled together the HtmlApp code that nikhilk wrote, as well as the csharpformat from manoli and bundled them together to produce this.  i had some issues with manoli's library when trying to set the embed stylesheet property, but i just embedded it as a resource and that was fine by me.  it was easy to implement and here is the magical function code:

public override void Load(FileInfo file)
{
    StreamReader rdr = file.OpenText();
    string previewCode = rdr.ReadToEnd();
    string formattedCode = FormatCode(previewCode, file.Extension);

    HtmlApp.Html.HtmlControl html = new HtmlApp.Html.HtmlControl();
    html.LoadHtml(string.Format("<html><head><style type=\"text/css\">{0}</style><body>{1}</body></html>", SmilingGoat.PreviewHandlers.Properties.Resources.CssString, formattedCode));
    html.Dock = DockStyle.Fill;

    Controls.Add(html);
}


after this (the FormatCode funciton basically just does a switch on the file type and applies the appropriate formatting from manoli's object), the formatted code is rendered to view like the sample above.  it's really easy to implement.

one thing important to note about preview handlers is the fact that only one preview handler can be associated with one file extension and class.  what does that mean? well, simple...for example, you can't have two pdf preview handlers.  well what happens if i install two of them? last one will likely win -- depending on how it is registering itself.  you see, there will only be one registry entry for that file type, so the second one you install will overwrite the settings from the first one.  stephen planned on posting a little utility to see what preview handlers are registered for your machine...check it out to see how things are registering and to actually add additional ones.  for example, if i didn't want code highlighting, i could have easily set the TXT preview handler to also preview the code files mentioned above.  here are the links to my code preview handler:

File iconCode Preview Handler [source]
File iconCode Preview Handler Setup [MSI]

  1. 12/14/2006 12:37 PM | # re: vista and office dev: creating preview handlers
    Nice handlers, Tim! I've put up the tool you mentioned at http://blogs.msdn.com/toub/archive/2006/12/14/preview-handler-association-editor.aspx
  2. 1/29/2007 4:59 PM | # re: vista and office dev: creating preview handlers
    Hi:
    I would like to create previewer for PDF files but am not a programmer. I installed Visual J# v2.0 but in attempting to install your MSI installer version I get the following error: Unable to get installer types in the C:...\codepreviewhandler.dll assembly. Unable to load one or more of the requested types. Can you give me any advice please.

    Also do I need to download anything from foxit?

  3. 1/29/2007 10:05 PM | # re: vista and office dev: creating preview handlers
    fred: ensure you have the .NET framework version 2 installed as well (should be there). send me the full error offline and i'll see -- i've not seen this reported before.

    regarding the pdf one...i am awaiting an updated control from foxit.
  4. 1/30/2007 12:25 PM | # re: vista and office dev: creating preview handlers
    Tim:
    The only thing I didn't include from the message was: Retrieve the LoaderExcceptions property for more information. I do have .NET v2.0 & Visual J# 2.0 installed. With regard to pdf one, do you expect Foxit will comply? Thanks, Fred
  5. 1/31/2007 8:43 AM | # re: vista and office dev: creating preview handlers
    fred: yes, i'm actually partnering with foxit to create it.
  6. 2/12/2007 3:09 PM | # re: vista and office dev: creating preview handlers
    great post!! i wanted to make the same reader; i'd love to know when you/foxit release the extension!
  7. 2/13/2007 10:45 AM | # re: vista and office dev: creating preview handlers
    Hi.
    I've tried to modify the
    PdfPreviewHandler.cs file but I get an error message below.
    Can someone tell me what I'm doing wrong?

    =========================================

    System.Runtime.InteropServices.COMException (0x80040154): Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
    at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
    at System.Windows.Forms.AxHost.CreateWithoutLicense(Guid clsid)
    at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)
    at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
    at System.Windows.Forms.AxHost.CreateInstance()
    at System.Windows.Forms.AxHost.GetOcxCreate()
    at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
    at System.Windows.Forms.AxHost.CreateHandle()
    at System.Windows.Forms.Control.get_Handle()
    at MsdnMag.PdfPreviewHandler.PdfAxHost.LoadFile(String fileName)
    at MsdnMag.PdfPreviewHandler.PdfPreviewHandlerControl.Load(FileInfo file)
    at MsdnMag.FileBasedPreviewHandler.Load(PreviewHandlerControl c)
    at MsdnMag.PreviewHandler.<MsdnMag.IPreviewHandler.DoPreview>b__2()
  8. 2/17/2007 9:04 PM | # re: vista and office dev: creating preview handlers
    JackO: the MSDN PDF preview handler requires you to have the Adobe Acrobat Reader installed -- i am DAYS away from releasing one that does not --
  9. 3/1/2007 6:44 PM | # re: vista and office dev: creating preview handlers
    Not able to install on Vista RTM. I'm getting error code 2869. I've tried this to no avail:
    http://blogs.conchango.com/pauloreichert/archive/2006/11/21/Windows-Installer-MSI-packages-error-code-2869-on-Windows-Vista.aspx
  10. 3/2/2007 4:50 PM | # re: vista and office dev: creating preview handlers
    Hi Tim,

    I'm trying to make a previewer to run in Outlook 2007, but without Vista. Do you have any ideas how to do that?

    TIA
  11. Gravatar
    3/5/2007 6:56 AM | # preview handlers in XP?
    hi,may i know if the File Preview Handler Setup [MSI] can be used in XP? Apparently, I have went ahead with the installation but no preview in outlook 2007 for pdf and html. thanks in advance!
  12. 3/26/2007 4:39 PM | # re: vista and office dev: creating preview handlers
    I get the same error as Jon Galloway, error code 2869. Any idea what is causing this or how to fix it?
  13. 4/4/2007 10:23 AM | # re: vista and office dev: creating preview handlers
    nope.. same error here.
  14. 4/4/2007 10:32 AM | # re: vista and office dev: creating preview handlers
    if you are installing on Vista with user account control enabled, oyu have to elevate to a command prompt and then run msiexe /i <pathtoMSI>

    i did change the MSI installer so it should prompt automatically, so you may want to re-download and try both methods.
  15. 4/13/2007 2:37 PM | # fgqufvmd - Google Search
    fgqufvmd - Google Search
  16. 4/20/2007 9:21 AM | # jemucfed - Google Search
    jemucfed - Google Search
  17. 5/19/2007 3:22 PM | # re: vista and office dev: creating preview handlers
    Hi,
    I've created a bunch of preview handlers, including those displayed here (I used Acrobat instead of Foxit), and put them (and their source code) in here:
    http://www.azarfamily.org/previewhandlersforwindowsxp

    Enjoy :)
  18. 5/29/2007 4:07 AM | # re: vista and office dev: creating preview handlers
    Is it possible to view the preview of files in a custom application in Vista.
    Not in the expolorer
  19. 6/7/2007 8:04 PM | # re: vista and office dev: creating preview handlers
    ok
  20. 2/2/2008 10:04 PM | # MSDN Blog Postings &amp;raquo; Preview Handlers in Vista and Outlook
    MSDN Blog Postings &amp;raquo; Preview Handlers in Vista and Outlook
  21. 12/14/2008 8:35 PM | # re: vista and office dev: creating preview handlers

    Looking for preview handler for .rar files? Thanks to the MSDN preview pack, I've got everything else covered EXCEPT .rar files...
  22. 1/26/2009 1:24 AM | # re: vista and office dev: creating preview handlers
    Is it possible to preview office documents and highlight specific words?
    Plus, i am looking for a preview for simple text files and eml (outlook express) files (container files)
  23. 1/26/2009 3:08 PM | # re: vista and office dev: creating preview handlers
    Yaniv by default simple text files (txt) should use the default preview handlers provided by Office 2007. As far as previewing documents and highlighting specific words, I'm sure someone could develop this, but how would it know which words to highlight?
  24. 6/17/2009 9:40 PM | # re: vista and office dev: creating preview handlers
    I am using window XP sp2, office XP (not 2007) & outlook 2007. Is it still possible to preview office documents (like .doc, .xls, .ppt etc) in outlook then ? Thanks for any comment in advance !
  25. 6/17/2009 9:47 PM | # re: vista and office dev: creating preview handlers
    Toad -- you state office xp, but outlook 2007...I'm confused...which is it? If you have Outlook 2007, you can use preview handlers, but the doc/xls/etc. may not be built in...you can check.
  26. 6/18/2009 3:12 AM | # re: vista and office dev: creating preview handlers
    Thanks for ur reply Tim. I installed office XP but separately installed outlook 2007 only. I checked there's no preview handler for doc/xls etc. So do I have to install the full pack of office 2007 (not just outlook) ?
  27. 6/18/2009 8:06 AM | # re: vista and office dev: creating preview handlers
    Toad -- Probably so. Honestly I'm not sure where the office handlers come from, but that's reasonable to suspect they are from the full Office 2007 install.
  28. 8/12/2009 10:39 PM | # re: vista and office dev: creating preview handlers
    Tim, I tried to download your Preview Handler [source]
    & Preview Handler Setup [MSI] from the box.net you mentioned but after i tried several times what my Mozilla Firefox found is :

    Address Not Found
    www.box.net could not be found. Please check the name and try again.

    I'm connected to the Internet, but not able to find the domain.
    does the domain still exist?
    I am expecting for your reply, or if you do not mind, please send me the file to my email. Thanks.
  29. 10/10/2009 4:09 PM | # code previewers working in Vista Explorer Preview pane, but not Outlook 2007
    Tim,
    I know you probably haven't done much with these lately, but I was wondering if this problem was just mine or if, perhaps, something had changed in Outlook (SP1 or SP2?).

    I have a few preview handlers installed, including your Code Preview Handler pack. For example, I can preview .js files in Vista's explorer preview pane, but in Outlook 2007, it says "This file cannot be previewed. Try opening the file in the program in which it was created."

    I'm running Vista Ultimate SP2 and MS Office Enterprise 2007: Outlook (12.0.6504.5000) SP2 MSO (12.0.6425.1000)

    When I use either PreviewConfig (by Ramesh Srinivasan), Preview Handler Association Editor (by Stephen Toub), or Outlook's Trust Center > Attachment Handling > Attachment and Document Previewers tool, they show that your "Source Code Preview Handler" (PreviewConfig shows it as TimHeuer.PreviewHandlers.CodePreviewHandler) is associated with .cs, .js, .sql, .vb.

    Any idea why it would work in Vista's explorer preview pane and not Outlook's preview pane?

    Thanks!
  30. 10/16/2009 3:03 PM | # re: vista and office dev: creating preview handlers
    David -- sorry, I'm not sure what could be causing that. It's the same mechanism for previewing on Vista. Sometimes, the file extensions are prevented in some Outlook configurations (i.e., we don't allow js attachments)
  31. Gravatar
    1/8/2010 1:55 PM | # mediaplayer auto view in 200% zoom
    Where can I setup the media player to automatically open the video in 200% zoom ?
  32. 1/10/2010 2:42 AM | # re: vista and office dev: creating preview handlers
    I got some problems with preview outlook 2007 before Vista OS ,can you come out with solution soon?
  33. 1/27/2010 9:55 AM | # Broken in XP?
    Like some others, I can't get the basic MSDN previewers or this code previewer to work in XP using Office 2007. The Office (Word, etc.) previewers work, but not these. Any suggestions for XP users (besides upgrading)?
  34. 3/18/2010 6:22 AM | # re: vista and office dev: creating preview handlers
    This Preview Handler is nice, but it doesn't support copy and paste. As work-around I made the file extensions that I needed to work with (a .sql file below) use the text files Preview Handler.

    Windows Registry Editor Version 5.00

    [HKEY_CLASSES_ROOT\sqlwb.sql.9.0\ShellEx\{8895b1c6-b41f-4c1c-a562-0d564250836f}]
    @="{1531d583-8375-4d3f-b5fb-d23bbd169f22}"

 
Please add 1 and 1 and type the answer here:
First time here? You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting! (hide this)