Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Word Control for .NET

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

Sample Image - winwordcontrol.jpg

Introduction

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.

Background

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.
winwordcontrolinstance.CloseControl(); 
This method closes the actual document. This is mostly not necessary because LoadDocument() can handle multiple calls without calling CloseControl() before.
winwordcontrolinstance.PreActivate(); 
Preloading. This method should be used if you don't want to wait in the main program until Word is finished starting.
winwordcontrolinstance.RestoreWord(); 
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:
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);

[DllImport("user32.dll")]
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,
	SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME );
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 www.intercompu.de. If you use this stuff or if you use just parts of it, I would appreciate if you mention my name. ;-)

History

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

License

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


Written By
CEO
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: www.intercompu.de

Comments and Discussions

 
Generalget the office documents on the server Pin
nichen100122-Dec-03 15:09
nichen100122-Dec-03 15:09 
GeneralWord Form Border Pin
Wyetela14-Dec-03 8:29
Wyetela14-Dec-03 8:29 
GeneralRe: Word Form Border Pin
Matthias Hänel21-Dec-03 15:54
Matthias Hänel21-Dec-03 15:54 
GeneralRe: Word Form Border Pin
Wyetela21-Dec-03 23:35
Wyetela21-Dec-03 23:35 
GeneralRe: Word Form Border Pin
Matthias Hänel23-Dec-03 7:48
Matthias Hänel23-Dec-03 7:48 
GeneralRe: Word Form Border Pin
Wyetela23-Dec-03 8:04
Wyetela23-Dec-03 8:04 
GeneralRe: Word Form Border Pin
Anonymous13-Feb-04 21:38
Anonymous13-Feb-04 21:38 
GeneralRe: Word Form Border Pin
Matthias Hänel13-Feb-04 21:40
Matthias Hänel13-Feb-04 21:40 
GeneralRe: Word Form Border Pin
Anonymous13-Feb-04 21:51
Anonymous13-Feb-04 21:51 
GeneralRe: Word Form Border Pin
Matthias Hänel13-Feb-04 21:57
Matthias Hänel13-Feb-04 21:57 
GeneralRe: Word Form Border Pin
Anonymous13-Feb-04 22:28
Anonymous13-Feb-04 22:28 
Generalusing two event at the same time using doEvent() Pin
passou8-Nov-03 9:08
susspassou8-Nov-03 9:08 
GeneralWord Macro Pin
Mahendra Nepali21-Oct-03 20:06
Mahendra Nepali21-Oct-03 20:06 
GeneralRe: Word Macro Pin
Matthias Hänel4-Nov-03 10:07
Matthias Hänel4-Nov-03 10:07 
QuestionCan I do this with other type of windows Pin
mlim197231-Jul-03 14:09
mlim197231-Jul-03 14:09 
AnswerRe: Can I do this with other type of windows Pin
Matthias Hänel1-Aug-03 1:20
Matthias Hänel1-Aug-03 1:20 
GeneralCan't use control w/ VB.Net Pin
Jeff Deville28-May-03 15:02
Jeff Deville28-May-03 15:02 
GeneralCannot find Assembly Interop.Word Pin
Silberztein25-May-03 23:20
Silberztein25-May-03 23:20 
GeneralRe: Cannot find Assembly Interop.Word Pin
Silberztein26-May-03 0:36
Silberztein26-May-03 0:36 
GeneralRe: Cannot find Assembly Interop.Word Pin
Matthias Hänel26-May-03 9:32
Matthias Hänel26-May-03 9:32 
GeneralI found a BUG in the programe under Office XP.... Pin
layerr12-May-03 6:20
layerr12-May-03 6:20 
Generalnice,but when it's over,one error happen. Pin
xhw12-Mar-03 15:41
xhw12-Mar-03 15:41 
GeneralRe: nice,but when it's over,one error happen. Pin
Christiaan van Bergen14-May-03 22:37
professionalChristiaan van Bergen14-May-03 22:37 
Questionwhen can the error that it's "Error: do not load the document into the control until the parent window is shown" happen? Pin
xhw18-Feb-03 7:32
xhw18-Feb-03 7:32 
AnswerRe: when can the error that it's "Error: do not load the document into the control until the parent window is shown" happen? Pin
Matthias Hänel18-Feb-03 21:19
Matthias Hänel18-Feb-03 21:19 

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.