Host Word in winform Application


We had a question in the VSTO Forums about hosting Word on a winform in .NET. I thought I would give some broader detail, just because I’m in the mood.


Here’s the deal: hosting Word (or really any other Office app) on a winform is different from automating the application from .NET code.
Here (from the www.CodeProject.com) is what hosting the app on a winform looks like:



Yes, in both cases a .NET process is used to launch the whole process, but there is a key difference: when hosting Word as an application on the winform, you need to use the SetParent win32 function and pass the hwnd of Word. In the other situation where you are automating the Word OM from managed code, you use methods that we document thoroughly on MSDN. It’s a cakewalk with our Visual Studio 2005 Tools for Office product.


As I just mentioned, the CodeProject.com has a project showing how to use SetParent properly and so on. However, it’s not a supported scenario. You can do it, but we (Microsoft) do not recommend it.


But, you have another option, you can use the Web browser control. Full details are here:


304662 – HOWTO: Use the WebBrowser Control to Open Office Documents with Visual C#
.NET
http://support.microsoft.com/default.aspx?scid=KB;en-us;304662


I have included two more KB articles here. One shows how to use the dsoFramer control for hosting documents. However, this is not meant to be used in managed code, so don’t go there. The second KB shows how to get the windows handle for your Office app. It can come in handy in certain situations.


311765 – SAMPLE: Visual C++ ActiveX Control for Hosting Office Documents…
http://support.microsoft.com/default.aspx?scid=KB;en-us;311765


302295 – HOWTO: Obtain the Window Handle for an Office Automation Server by Using
Visual C# .NET
http://support.microsoft.com/default.aspx?scid=KB;en-us;302295


 Rock Thought for the Day: OK- I have been wanting to blog about System of a Down- Mesmerize/Hypnotize, but I have had other rock topics to address. No one can possibly argue that B.Y.O.B is not an infectious song. It’s the best (perhaps only) example of Metal/Pop in existence. It’s pure bubble gum….filled with razor blades. Fair enough. I love the tune. The video is decent, but not great. Now, the overall opus- both albums- continually reminds that their theme has already been addressed with more memorable songs back in the 70’s., by another prog-rock group, in a double-album opus, full of despair and a lingering expression of hope, imbued with esoteric symbology, jack-booted marching soldiers, and ultimately more memorable songs: Pink Floyd- The Wall. The only difference is that System of a Down are overtly self-referential in their critique of media culture and it’s power to make us all automatons of destruction. Fine. They get some points for “honesty”. There is no question that the guitar work is freakishly good. The speed is commendable, and the songs are complex. They get points for musicianship and originality. But, put a David Gilmore solo next to it, and you are compelled to think, “Welcome, my son. Welcome to the machine.”


Rock On

Comments (3)

  1. john says:

    u have undertaken lots of effort to write this. This is simply great mate, keep the good work going!!

  2. johnrdurant says:

    Thanks a million!

  3. Joshua says:

    How can I (using a windows form application) to upload and download MS Word documents into sql server?  I have been able to do this without a problem in ASP.net pages, but I can’t figure out how to do this using Winforms.  Basically, I have been able to read a byte array containing the data into a memorystream object, but I can’t figure out how to get the data from that stream object into the word document.

    Can you provide any information or reference articles regarding this topic, please?

    Thank You.