Click here to Skip to main content
15,179,684 members
Articles / Programming Languages / C#
Posted 28 Jan 2003


194 bookmarked

Word Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.85/5 (50 votes)
28 Jan 2003GPL33 min read
Writing a control to use MS-Word in your own Application like a windows-form.

Sample Image - winwordcontrol.jpg


This control allows you to use MS Word 2000 in your own projects such as a windows form. Of course that's a little "dirty trick". But if you have some documentation to display and you want to use Word for it, here is the solution.


I was working on an internal research project and we developed a tool to teach C++, and there was a problem about how to display the exercises. The main problem was, I had difficult formulas in these descriptions so the only possible solution was to convert the files to PDF or to another file format. With PDF I had a problem, because I couldn't edit the documents afterwards. So, I was looking for an ActiveX or .NET control for MS Word. Finally, I couldn't find one. So I decided to write one for .NET.

How to use the control?

It's almost too easy to use the form. You just need to add a link to the winwordcontrol.dll.  The steps are:

  1. Goto your Toolbox.

    Visual Studio Toolbox

  2. Click right. Select "Customize Toolbox".

    Toolbox Cutomizing

  3. Select the ".NET Components" tab.
  4. Select the winwordcontrol.dll using the file browse dialog. Click OK. After that you will find a new form in your toolbox called "winwordcontrol".

    Toolbox winwordcontrol/winwordcontrol

Drag and drop it just like any other form. The only thing you should know is how to control it. At this moment, there are four methods:

winwordcontrolinstance.LoadDocument(<string path>); 
With LoadDocument you can load all files Word can handle. Please specify the complete path! This is the only method that is probably useful for everyone. All the other methods are not necessary for normal use.
This method closes the actual document. This is mostly not necessary because LoadDocument() can handle multiple calls without calling CloseControl() before.
Preloading. This method should be used if you don't want to wait in the main program until Word is finished starting.
Reactivates all the Menubars.

The control will automatically start Word if it's not already open and it will display the document.

How does this work?

The basic idea is to instantiate the Word application, as everybody knows, with:

wordInstance = new Word.Application();
Now we have the Word application in a separate window. How do we get this window in our control? The answer is ... Win32 API. We need to import some API functions from user32.dll as shown here:
public static extern int FindWindow(string strclassName, string strWindowName);

static extern int SetParent(int hWndChild, int hWndNewParent);

[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
	int hWnd,               // handle to window
	int hWndInsertAfter,    // placement-order handle
	int X,                  // horizontal position
	int Y,                  // vertical position
	int cx,                 // width
	int cy,                 // height
	uint uFlags             // window-positioning options
[DllImport("user32.dll", EntryPoint = "MoveWindow")]
static extern bool MoveWindow(
	int hWnd, 
	int X, 
	int Y, 
	int nWidth, 
	int nHeight, 
	bool bRepaint
const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
This is the most complicated part... because it's NOT documented at all. We need a Pointer to the Word window, so we use FindWindow to find it. What's the name of the window? - "Opusapp" Actually, I don't know what this name means, but it works perfectly.
wordWnd = FindWindow( "Opusapp", null);
So, now we can define our control as a parent to Word:
SetWindowPos(wordWnd, this.Handle.ToInt32(), 0, 0,
	this.Bounds.Width-20, this.Bounds.Height-20,
Everything else should be pretty easy and it is well documented in the official documentation for the Word namespace.

Points of Interest

You may be interested in my other projects. Please visit my private homepage If you use this stuff or if you use just parts of it, I would appreciate if you mention my name. ;-)


  • 23.01.03 First release
  • 25.01.03 Update minor changes (performance enhancement)


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Matthias Hänel
Germany Germany
I am a developer with a Computer Science degree. I am programming Visual C ++ for over 7 year now and C# for about 3 years. If you want to know more about me please look at my homepage:

Comments and Discussions

GeneralHighLighting a selected text in WinWordControl Pin
pramod.21c25-Nov-05 0:02
Memberpramod.21c25-Nov-05 0:02 
GeneralRe: HighLighting a selected text in WinWordControl Pin
JayashreeG2-Jan-06 2:05
MemberJayashreeG2-Jan-06 2:05 
GeneralRe: HighLighting a selected text in WinWordControl Pin
pramod.21c10-Jan-06 18:06
Memberpramod.21c10-Jan-06 18:06 
GeneralRe: HighLighting a selected text in WinWordControl Pin
JayashreeG11-Jan-06 16:16
MemberJayashreeG11-Jan-06 16:16 
QuestionCan be used in vc6.0 Pin
Fzz17-Nov-05 2:44
MemberFzz17-Nov-05 2:44 
GeneralCompiling the project in .NET 2003 Pin
udivaks26-Sep-05 23:29
Memberudivaks26-Sep-05 23:29 
GeneralRe: Compiling the project in .NET 2003 Pin
fferdinanvi22-Nov-05 14:27
Memberfferdinanvi22-Nov-05 14:27 
NewsImproved OnResize implementation Pin
John Fisher16-Sep-05 12:51
MemberJohn Fisher16-Sep-05 12:51 
Using the following code, you can have a much more precise resizing of the Word window. (At least it works very well for Word 2003.) The algorith uses only SystemInformation values, so it should work across OS versions, and probably across Word versions.

private void OnResize(object sender, System.EventArgs e)
    int borderWidth = SystemInformation.Border3DSize.Width;
    int borderHeight = SystemInformation.Border3DSize.Height;
    int captionHeight = SystemInformation.CaptionHeight;
    int statusHeight = SystemInformation.ToolWindowCaptionHeight;
        -2*borderHeight - captionHeight, 
        this.Bounds.Width + 4*borderWidth, 
        this.Bounds.Height + captionHeight + 4*borderHeight + statusHeight,


"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.

-- modified at 17:52 Friday 16th September, 2005
GeneralSystem.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. Pin
boozedemon16-Aug-05 20:15
Memberboozedemon16-Aug-05 20:15 
GeneralRe: System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. Pin
John Fisher1-Sep-05 12:13
MemberJohn Fisher1-Sep-05 12:13 
GeneralRe: System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable. Pin
Rakesh B Singh23-Feb-07 1:01
MemberRakesh B Singh23-Feb-07 1:01 
GeneralError dropping control on form Pin
Pierre_Anderson19-Jul-05 4:55
MemberPierre_Anderson19-Jul-05 4:55 
Generalcongratulations, i have a problem with your control Pin
mclameiro18-Jun-05 10:06
Membermclameiro18-Jun-05 10:06 
GeneralVB.Net Interop.Word (How can I classified the word as (noun, pronoun, adj etc.) Pin
zrialkiller30-May-05 21:44
susszrialkiller30-May-05 21:44 
GeneralRun more than one instance Pin
lmaks24-May-05 22:29
Memberlmaks24-May-05 22:29 
GeneralRe: Run more than one instance Pin
apa12320-Dec-05 23:47
Memberapa12320-Dec-05 23:47 
GeneralRe: Run more than one instance Pin
bruegge14-Mar-06 3:56
Memberbruegge14-Mar-06 3:56 
GeneralRe: Run more than one instance Pin
sarawutt5-Jan-20 23:34
Membersarawutt5-Jan-20 23:34 
Generalcan't compile source code.. Pin
udiddy41230-Mar-05 7:46
Memberudiddy41230-Mar-05 7:46 
GeneralRe: can't compile source code.. Pin
jimmi4u3-Apr-05 1:05
Memberjimmi4u3-Apr-05 1:05 
GeneralRe: can't compile source code.. Pin
lmaks19-May-05 4:29
Memberlmaks19-May-05 4:29 
QuestionHow can I get the document object in this word control?:( Pin
davidsoftware19-Jan-05 1:28
Memberdavidsoftware19-Jan-05 1:28 
AnswerRe: How can I get the document object in this word control?:( Pin
Matthias Hänel19-Jan-05 10:45
MemberMatthias Hänel19-Jan-05 10:45 
AnswerRe: How can I get the document object in this word control?:( Pin
ozie15-Feb-06 17:00
Memberozie15-Feb-06 17:00 
GeneralGreat control! Pin
Alan C. Balkany7-Jan-05 6:11
MemberAlan C. Balkany7-Jan-05 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.