Hosting Office in a WPF Application

note 

Updated on December 17, 2009

The approach described in this article for hosting office documents in WPF is no longer supported.  Microsoft has recently pulled the DSO Framer control from its knowledge base.  🙁

Please contact Microsoft Developer Support directly if you have questions regarding this decision.

 If a new supported solution for hosting Microsoft Office applications within WPF becomes available, I will try to post a sample.

Cheers,
Dr. WPF


Dear Dr. WPF,

Is it possible to host Microsoft Excel inside of a WPF application? Do you have any sample code?

Sincerely,
Rob
 


Hi Rob,

It’s certainly possible to host a Microsoft Office application inside of a WPF application. And just to prove it, I’m writing this entire post inside of Microsoft Word while it’s being hosted inside my WPF application.

WPF Hosting WordFeel free to download the code for this sample. This sample will work for hosting Word, Excel, PowerPoint, or Visio documents. It should host Project documents also, but I don’t have Project, so I couldn’t verify this. And I should note that hosting Visio 2007 documents proved to be very flaky. The Visio host crashed more often than not. The other hosts seemed pretty stable.

You will need to install the DSO Framer control prior to running the sample. More on that below…

So yes, it’s possible to host an Office application, but that doesn’t necessarily mean it’s easy! There are definitely a lot of caveats, disclaimers, qualifiers, stipulations, and limitations (yep, even the thesaurus works in the hosted scenario!) when hosting any Win32-based window (a.k.a., an HWND) within a WPF app. Most of these are documented in the SDK in topics like WPF Interoperation: ”Airspace” and Window Regions Overview and Hosting a Microsoft Win32 Window in WPF. In the latter article, special attention should be given to the sections entitled “Notable differences in output behavior” and “Notable differences in input behavior”.

Hosted Office applications come with a whole set of their own challenges (keeping the document focused, keeping application menus in sync with document menus, etc). If you go down the path of hosting an Office app, you should expect some technical challenges and a potential steep learning curve, especially if you’re considering office automation.

There are code samples in the SDK that demonstrate how to host an ActiveX control inside your WPF application. So if you can find a simple ActiveX wrapper for your Office application, then you’re all set, right?

Well, unfortunately it’s not that easy when it comes to hosting Office. An Office application, like Excel, has much larger requirements for its host container. The host must be an Active Document Container. This container implements a number of COM interfaces above and beyond those found in a simple ActiveX host.

There are a couple of Active Document Containers readily available on most Windows machines: 

1. Internet Explorer

2. the Windows Forms WebBrowser control

The simplest approach to hosting an Office application inside a managed application involves hosting the Windows Forms WebBrowser control and pointing it at an Office document. If all you care about is simple hosting, this approach might work for you.

SIDEBAR: For the record, I should point out that there is also an unmanaged WebBrowser control (shdocvw.dll) that ships as part of Internet Explorer. This control can be hosted in an unmanaged app (or even in a managed app using the instructions in this article, but why would you do that when there’s already a managed control?). Indeed, the Windows Forms WebBrowser control is just a managed wrapper around the Internet Explorer WebBrowser control. As such, Internet Explorer must be installed for any solution that involves either WebBrowser control.

Usually, however, if you are hosting something like Excel, you need to automate the document (to load, save, or print it, invoke automation commands, etc). The WebBrowser control does not give you much access to the document-related automation classes. (For information on the Office automation classes, check out the vast Office Development documentation in the SDK.) For this level of control, you need a better Active Document Container. Unfortunately, Microsoft has not released any such “supported” container. It would be great if they could put out a managed control that could serve as an Active Document Container. But so far, the only thing they offer is an “unsupported” container in the form of an ActiveX control called DSO Framer control.

The DSO Framer control was produced a few years back by the good folks in Microsoft Developer Support. Although their name says it (“Support”, that is), they don’t do it (“Support”, that is) for this control. The DSO Framer control (along with its source code… yes, you get the full source) is provided “as is”. Microsoft doesn’t support it and Dr. WPF doesn’t support it either… However, we both use it!

My sample of WPF hosting Word (illustrated above and available for download) is indeed built using the DSO Framer control. It is an extremely simple implementation of a WPF application hosting an ActiveX control. In this case, that ActiveX control just happens to be an Active Document Container.

NOTE: I could not get the most recent version of the DSO Framer control (compiled as a 32-bit control) to run on my 64-bit Vista box. It runs well on my 32-bit Vista installation. On my x64 machine, the application complains that the control is not registered, even though I can verify and instantiate an instance just fine in OleView. Not sure if this is a problem with the control or a configuration problem on my machine. If others encounter the same problem, I’d love to hear about it. Maybe someone in Microsoft Developer Support could try to reproduce this failure and look into a fix (hint, hint)… Oh yeah… it’s not a supported control. Maybe if I say “pretty please”.

FIX: Several people have advised that compiling the WPF application to specifically target the x86 platform (rather than “Any CPU”) fixes the above load issue on x64.  Thanks to all for this fix!  😀

Anyway, I hope you find this information and sample helpful!

Best regards,
Dr. WPF

38 Responses to “Hosting Office in a WPF Application”

  1. Office Geek says:

    Perfect. Exactly what I was looking for!

  2. Hari Prasad Kakkirani says:

    Hi Dr. WPF,

    Thanks for providing a solution for hosting an office document in WPF.
    Same as you mentioned above, i’m having the problem in registering this control on X64 Vista Machine, I checked it on XP it’s working perfect fine except while changing windows.

    Did you come across solution with this…if so, can you please share with me?

    on XP machine, when we change the documents with Alt+Tab, toolbars are not visible in the first attempt, but when we move the mouse on it, then it’s getting displayed…

    how to avoid that problem?

    Please help me with these two problems.

    Thanks
    Hari.

  3. Dr. WPF says:

    Hi Hari,

    No, I have not heard of a solution for using the DSO Framer control on x64. 🙁 I think Microsoft would have to address this particular issue. I have seen it with other ActiveX controls also, so I don’t think it is specific to the Framer control.

    For the second problem, I found that I could cause the document host to refresh its UI by calling the Activate() method on the framer control, so I use that approach as a hack when resizing the host. It might also work when switching documents via Alt+Tab. Just a thought…

    Cheers,
    -dw

  4. Manju says:

    I’m trying to open a .docx file (having a Action Pane) in DSO Framer and am unable to see any of the Action Pane items. How can I fix it? Is this the inability of DSOFramer or am I not setting the Word attributes properly? Please let me know.

  5. Dr. WPF says:

    Hi Manju,

    I wish I knew the answer to your question, but I really don’t know much about office automation or the capabilities of the Office 2007 document host. My guess is that this is not a limitation of the framer control, but rather some document property that needs to be set (but that really is just a guess). I think you’d have to contact the team at Microsoft that supports office development to get a definite answer.

    Best regards,
    -dw

  6. Uffe Roenne says:

    Hi Doc

    Thanks for the brilliant example.

    It seems to have the same bug as my own. Try and do the following:

    – Open up a Word (2003) inside the control
    – Open up Word as you would normally do it, and close it again

    The toolbar of the Word inside the control is now dead.

    Any ideas?

    Regards

    Uffe

  7. Cornel says:

    Many links from this article are not active 🙁 ex: download code

  8. Dr. WPF says:

    Cornel, I just did a quick test of all the links within the post and they all worked for me, including the link to download the code. Please try again and if you still cannot download the code, send me an email and I’ll forward the sample to you.

  9. Cornel says:

    it seems that in Firefox doesn’t work, in IE everything OK

    Thanks

  10. Cornel says:

    Now I get this error on runnig:
    “Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))”

    What seems to be the problem?

    Thanks

  11. Dr. WPF says:

    Cornel, It sounds like you might be running on a 64-bit machine. The symptoms are the same as the problem described in nearly the last paragraph of this post (see “NOTE:” above).

    As far as I know, only Microsoft would be able to address the 64-bit problem. 🙁

  12. Dr. Spain says:

    To fix toolBar problems using DSOFramer OCX you should set FrameHookPolicy to dsoSetOnFirstOption at design time

  13. Jim says:

    I really like your application. I am trying out Visual Studio 2008 beta and upon opening did a Re-build and changed the .Net FrameWork from 3.0 to 3.5. The rebuild works real well on my Vista Home PC.
    I have tried Microsoft’s examples which seem to be very tempormental – whereas yours runs very smooth. I haven’t tried it on other machines but looks great so far.
    thanks for posting this solution.
    Jim

  14. Dirk says:

    You should be able to run the application on x64 if you set the project’s target platform to x86 instead of AnyCPU.
    This is required for all applications that use native x86 binaries (COM or PInvoke).

  15. Dr. WPF says:

    Thanks Dirk! That sounds very promising. (Unfortunately, I’m now running 32-bit Vista on my 64-bit box, so I can’t easily test it, but hopefully it helps others! :))

  16. Franc Krueger says:

    I received the REGDB_E_CLASSNOTREG error as well on my windows machines that run XP64. I just changed the build configuration to target a x86 machine and it works fine.

  17. MC says:

    Can this control be adjusted to display a PowerPoint slide show without launching PowerPoint? I am looking to display ppsx files as slide shows in my wpf application.

  18. Dr. WPF says:

    Hi MC,

    The DSO Framer control can only be used to host an entire office application. So you can definitely host PowerPoint, but this is going to give you the full blown app.

    There may be another ActiveX control somewhere out there that could be used to view just a slide show. I’m afraid that’s not really my area of knowledge, so I can’t give you a definitive answer.

  19. Jatin says:

    Thanks for the article. it was very helpful although i am facing one problem. While using the DSO Framer control in a windows application, i am unable to include it in the setup/deployment project for that application. Basically i am creating a setup for the application so that i can install it on another machine. I need to install the dso Framer control on the other machine separately. How can i include it within the main application setup?

  20. Dr. WPF says:

    Sorry Jatin. That’s just not my area. 🙁

  21. Jasvinder says:

    Hi Doctor,
    I used your Dsoframer in xbap(xp sp2 x86) and working greate.But same project gives exception(REGDB_E_CLASSNOTREG) on another machine (xp sp2 x86).
    Any Idea?

  22. Dr. WPF says:

    Sorry Jasvinder… DSO Framer is not my control… you would have to work with Microsoft to determine why its failing.

  23. Larry says:

    Have you tried recompiling the DSOFramer targeting the x64 platform? I’m trying now, but I’m not experienced with cpp or 64-bit migration and getting all sorts of compile errors that I don’t understand. Wanted to know if you tried.

  24. Dr. WPF says:

    I don’t think I ever tried recompiling DSOFramer for x64. I was just using it “as-is”.

    Again, the 32-bit version should work fine on x64 since it loads and operates in OleView. According to Dirk’s suggestion, it might work with a WPF app if you simply recompile the WPF app targeting x86 instead of AnyCPU.

  25. richhumph says:

    Hi
    Nice sample – very handy thanks

    I had the same problem as Jasvinder – to resolve it I downloaded the latest DSOFramer package & re-reg’d the .ocx

    Microsoft Developer Support Office Framer Control 1.3 Sample (KB 311765)

    Latest DSO Framer Download

    then

    regsvr32 C:DsoFramerdsoframer.ocx

  26. Seshachalam says:

    DsoFramer giving error when placed in tab control and can i open multiple documents at a time.

  27. seshu says:

    I find problem with dsoframer in not showing the data in word document if the window property allowtransparancy is set to true

  28. basilkot says:

    it’s very strange, but i cannot set framer to automatically show my presentation immediately after I open it.

    Can someone help me with this problem?

  29. kwafir says:

    i tried this with excel viewer (no office installed)

    result is this error:

    The associated COM server doesn’t support Activex document embeding

    Can someone help me?

  30. Willy Chua says:

    I am using VS2008 on XP 64. And I got the REGDB_E_CLASSNOTREG error as described by others above. Turns out you need to changed the cs build property’s Platform target to x86. Then it starts working.

  31. Wildertalent says:

    Hi

    I follow the link for the DSO Framer Control above and it is not found – any ideas where I can get it from please

    Thanks

  32. Dr. WPF says:

    Bummer Wildertalent! Looks like they’ve pulled the DSO Framer Control. 🙁

    Looks like they are preparing for the release of Office 2010 and no longer wish to support the Active Document Container model. You will have to contact Microsoft Developer Support directly regarding this decision.

    If a new hosting solution becomes available, I will try to provide a sample.

  33. Ram says:

    Hi Dear Dr. WPF,
    This example is really help full to me and i appreciate your effort to create such an sample for us..
    i wanted to ask you one thing is it possible to invisible the save button in toolbar.because my requirement is to save the documents in database rather then file system .

    Thank’s
    Ram….

  34. gepi says:

    Running VS2005 on Vista64, I was getting the same error;
    I changed the Active solution platform to x86 as explained above and voilà ! (in Configuration Manager to which you get by right-clicking on your Solution in Solution Explorer. Note that selecting New then x64 did not work..)

  35. Dr. WPF says:

    Hi Ram,

    It’s pretty easy to get rid of the WPF save button, but I have a feeling you’re asking about the button within the Office toolbar. I honestly don’t know. I’m not an Office developer. You’d have to consult the documentation for that.

    Cheers,
    -dw

  36. gepi says:

    Ram: at least you can disable it (with set_EnableFileCommand)..

    ‘Setup the framer and commandbars options
    With mFramer
    .Titlebar = False
    If isEditor Then
    ‘show Word functionality except for the File menu buttons (handled by System)
    .Menubar = True
    .Toolbars = True
    pnltoolbar.Visible = False
    .set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileNew, False)
    .set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileOpen, False)
    .set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileSave, False)
    .set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileSaveAs, False)
    .set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileClose, False)
    ElseIf User.AccessLevel = SENIOR_ADMINISTRATOR Then
    ‘if user is a senior admin in Foreign Investment and doesn’t have the extra profile (template editor) ,
    ‘hide the Create Autotext Entry button
    .Menubar = False
    .Toolbars = False
    pnltoolbar.Visible = True
    btnCreate.Visible = False
    Else
    Throw (New Exception(“Your access level does not include Template editing”))
    End If
    End With

    Note: pnltoolbar is a custom ‘toolbar in that app.
    Hope this example is useful to you..

  37. Ram says:

    Hi DW,
    Exactly i am looking for the button with in the office toolbar..i will look in to that and one more thing i saw is when i opened and visio doc and then i click on the close button in wpf App toolbar then it is throwing an exception any ways i will look in to that too…

    Thank’s
    Ram

  38. Dr. WPF says:

    Hey Ram,

    You might take a look at gepi’s comment above, which indicates a way to disable buttons within the Office toolbar (thanks gepi! :)).

    The exception when hosting Visio doesn’t surprise me. As I noted, Visio was very flaky for me too when hosting it within the DSO Framer control. Not sure if its a problem with the Framer control or Visio or some combination thereof. Feel free to post any workarounds/findings here for others who might be interested. 🙂

    Thanks,
    -dw