Sunday, October 12, 2008   Search 
Links

 

Disciple
 Hosting Office in a WPF Application Minimize
Location: BlogsAsk Dr. WPF    
Posted by: Dr. WPF 8/24/2007 4:01 PM

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”.

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

Best regards,
Dr. WPF

 

Permalink |  Trackback

Comments (20)   Add Comment
Re: Hosting Office in a WPF Application    By Office Geek on 8/26/2007 2:40 PM
Perfect. Exactly what I was looking for!

Re: Hosting Office in a WPF Application    By Hari Prasad Kakkirani on 12/13/2007 3:50 PM
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.

Re: Hosting Office in a WPF Application    By Dr. WPF on 12/17/2007 12:06 PM
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

Re: Hosting Office in a WPF Application    By Manju on 12/19/2007 2:41 PM
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.

Re: Hosting Office in a WPF Application    By Dr. WPF on 12/19/2007 8:56 PM
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

Re: Hosting Office in a WPF Application    By Uffe Roenne on 1/11/2008 4:34 AM
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

Re: Hosting Office in a WPF Application    By Cornel on 1/18/2008 12:43 AM
Many links from this article are not active :( ex: download code

Re: Hosting Office in a WPF Application    By Dr. WPF on 1/18/2008 12:49 AM
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.

Re: Hosting Office in a WPF Application    By Cornel on 1/18/2008 3:12 AM
it seems that in Firefox doesn't work, in IE everything OK

Thanks

Re: Hosting Office in a WPF Application    By Cornel on 1/18/2008 3:18 AM
Now I get this error on runnig:
"Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"

What seems to be the problem?

Thanks

Re: Hosting Office in a WPF Application    By Dr. WPF on 1/18/2008 11:20 AM
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.

Re: Hosting Office in a WPF Application    By Dr. Spain on 2/8/2008 12:18 AM
To fix toolBar problems using DSOFramer OCX you should set FrameHookPolicy to dsoSetOnFirstOption at design time

Re: Hosting Office in a WPF Application    By Jim on 2/9/2008 10:13 PM
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

Re: Hosting Office in a WPF Application    By Dirk on 2/20/2008 3:35 PM
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).

Re: Hosting Office in a WPF Application    By Dr. WPF on 2/21/2008 5:30 AM
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! :))

Re: Hosting Office in a WPF Application    By Franc Krueger on 3/5/2008 12:18 AM
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.

Re: Hosting Office in a WPF Application    By MC on 5/15/2008 10:31 AM
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.

Re: Hosting Office in a WPF Application    By Dr. WPF on 5/15/2008 10:39 AM
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.

Re: Hosting Office in a WPF Application    By Jatin on 6/30/2008 2:34 PM
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?

Re: Hosting Office in a WPF Application    By Dr. WPF on 6/30/2008 2:40 PM
Sorry Jatin. That's just not my area. :(


Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 

  
Copyright 2007 by Dr. WPF   Terms Of Use  Privacy Statement