17

How do I use the word editor in a WPF application? Is it possible using windows forms hosting in WPF only? Is there another way to accomplish that?

I found AvalonEdit but it does not have features that I need. So using this way, my problem may not be solved.

Also there is some stuffs out there to host a windows forms control in WPF, but it could not be my answer.

I want to understand that is there a way to use word editor in a native way in a wpf app? Will all APIs be available in that solution?

Thanks in advance.

Drew Gaynor
  • 7,733
  • 5
  • 36
  • 50
Mostafa Rezaei
  • 470
  • 1
  • 5
  • 12

2 Answers2

12

You can host MS Word (2007/2010 and probably other versions) from within a WebBrowser control, this works in WinForms and should work in WPF too. A .NET API is provided for automating Word, documented here. The required interop assemblies ship with Office 2010, so deployment is a lot simpler than previous Office versions.

See this Microsoft Support article for more details on hosting Word within a WebBrowser control. The Screenshot below shows Word embedded within a host Winforms application.

enter image description here

Note that this only works reliably for a single hosted instance of Word, so you can't show 2 Word documents side by side in the same application. Also, the Ribbon can sometimes go missing - but Word hasn't ever caused the application to crash.

Administrative rights are required to make the required registry updates as there are potential security issues. One easy method to make the registry updates is to write a script, but the following (revised/untested) code shows how this can be done in c# for Word, Excel and PowerPoint:

using System.Security.AccessControl;

    private Dictionary<string,uint> OfficeBrowserRegKeys()
    {
        string[] officeRegKeyArray = new string[]
            {
                @"SOFTWARE\Classes\Word.Document.12",
                @"SOFTWARE\Classes\Word.DocumentMacroEnabled.12",
                @"SOFTWARE\Classes\Excel.Sheet.12",
                @"SOFTWARE\Classes\Excel.SheetMacroEnabled.12",
                @"SOFTWARE\Classes\Excel.SheetBinaryMacroEnabled.12",
                @"SOFTWARE\Classes\PowerPoint.Show.12",
                @"SOFTWARE\Classes\PowerPoint.ShowMacroEnabled.12",
                @"SOFTWARE\Classes\PowerPoint.SlideShow.12",
                @"SOFTWARE\Classes\PowerPoint.SlideShowMacroEnabled.12"
            };
        Dictionary<string,uint> officeRegKeys = new Dictionary<string, uint>();
        uint wrdVal = 0x80000024;
        uint excelVal = 0x80000A00;
        uint powerPtVal = 0x800000A0;
        foreach(string keyName in officeRegKeyArray)
        {
            if (keyName.Contains("Word"))
            {
                officeRegKeys.Add(keyName, wrdVal);
            }
            else if (keyName.Contains("Excel"))
            {
                officeRegKeys.Add(keyName, excelVal);
            }
            else
            {
                officeRegKeys.Add(keyName, powerPtVal);
            }
        }
        return officeRegKeys;
    }

    private void setNewOfficeKeys()
    {
        uint editFlag = 0x00010000;
        Dictionary<string,uint> officeRegKeys = OfficeBrowserRegKeys();

        foreach (KeyValuePair<string, uint> kvp in officeRegKeys)
        {
            try
            {
                RegistryKey rKey = Registry.LocalMachine.OpenSubKey(kvp.Key, 
                   RegistryKeyPermissionCheck.ReadWriteSubTree,
                   System.Security.AccessControl.RegistryRights.SetValue);
                rKey.SetValue("BrowserFlags", unchecked((int)kvp.Value),
                RegistryValueKind.DWord);
                rKey.SetValue("EditFlags", unchecked((int)editFlag),
                RegistryValueKind.DWord);
            }
            catch (Exception e) { string msg = e.Message; }
        }
    }
pgfearo
  • 1,945
  • 1
  • 19
  • 25
  • 1
    Thanks a lot @pgfearo. The solution approximately is what I am seeking for, but actually I want to host word editor only, without its ribbon control. As a matter of fact your web browser hosting solution doesn't provide my required control on the editor itself. Using that way, I will have problems with controlling the low level editor features, for example the page margin or getting bookmarks on the editor. How are you working with Word in your application, @pgfearo? thanks in advance. – Mostafa Rezaei Jun 23 '11 at 07:50
  • 2
    @Mostafa Rezaei. Yes, the above is only used as a doc viewer in my app. You get much more control actually hosting your app within Word as a COM addin, I did this in another concept project - [project video](http://www.youtube.com/watch?v=SpmgHgogftU). The [Microsoft documentation] (http://msdn.microsoft.com/en-us/vsto/dd164295.aspx) provides details on this approach, using Visual Studio 2010. – pgfearo Jun 23 '11 at 15:05
1

Well, Word proper isn't technically designed to be hosted by another app, whether it's WPF, WINFORMS or anything else.

You CAN use api tricks (like SetParent) to move the Main Word window into a WPF hosted window. I've done it before, but it's pretty tricky business and it's very easy to miss things that cause GPFs (both in Word and your app).

Is there any reason why it needs to be "Word in your app"? Why not write a little word addin and then launch Word from your app when necessary. then the Addin can communicate with your app, or your DB or whatever as necessary from within Word.

Users may find that to be a more usable approach in any case.

DarinH
  • 4,768
  • 2
  • 20
  • 32
  • 3
    Word is designed to be hosted by other apps, e.g. outlook by using OLE – Ian Ringrose Jun 02 '11 at 16:20
  • My app use open xml format to maintain text, I thought that maybe a better solution could be using the word editor to produce this text. but I need some control over the editor e.g. bold some text, etc. I searched over the Internet and found that using DSO Farmer hosting, there exist more control on the editor. I could not find the DSO Farmer ... – Mostafa Rezaei Jun 07 '11 at 03:36
  • I have done this by using DSO framer but it would have many unsolved problems. Maybe it's better to write a word addin. – Mostafa Rezaei Feb 10 '12 at 15:26
  • 1
    @Ian, Actually, the Outlook use of Word as email editor proved so onerous and problematic that MSOffice 2010 did away with it entirely. The Outlook editor is now a very stripped down version of word, completely separate from WinWord itself. – DarinH Feb 11 '12 at 19:29