Click here to Skip to main content
11,708,312 members (71,006 online)
Click here to Skip to main content

Word Control for .NET

, 28 Jan 2003 GPL3 611.2K 34.7K 191
Rate this:
Please Sign up or sign in to vote.
Writing a control to use MS-Word in your own Application like a windows-form.
<!-- Download Links --> <!-- Article image -->

Sample Image - winwordcontrol.jpg

<!-- Add the rest of your HTML here -->

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. Wink | ;-)

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)

Share

About the Author

Matthias Hänel
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

You may also be interested in...

Comments and Discussions

 
Questionhow to get the current page number Pin
htft013029-Jan-15 15:52
memberhtft013029-Jan-15 15:52 
Questionhow can i add merge field within this control? Pin
alexander ocampo hidalgo23-Dec-14 2:47
memberalexander ocampo hidalgo23-Dec-14 2:47 
Questiondoes not work in visual studio 2012 and not with office 2010 Pin
g_g_4-Jul-14 17:21
memberg_g_4-Jul-14 17:21 
QuestionHelp, please !!! I can't execute your demo program in my computer. Pin
DUBAR Fred29-Apr-13 4:53
memberDUBAR Fred29-Apr-13 4:53 
QuestionWhere to found 64 bit version DLL Pin
Pritesh Aryan28-Apr-13 17:29
memberPritesh Aryan28-Apr-13 17:29 
GeneralThanks a lot Pin
mechanicalsoldier13-Aug-12 17:57
membermechanicalsoldier13-Aug-12 17:57 
Questionit doesn't work with me Pin
mechanicalsoldier8-Jun-12 12:11
membermechanicalsoldier8-Jun-12 12:11 
AnswerRe: it doesn't work with me Pin
Babasaheb Dhodad28-Mar-13 23:50
memberBabasaheb Dhodad28-Mar-13 23:50 
Questionhow to ms word 2007 2010 ? Pin
ubattulga23-May-12 18:51
memberubattulga23-May-12 18:51 
QuestionIf this leaves open WinWord.exe processes behind Pin
Bernhard23-Feb-12 5:42
memberBernhard23-Feb-12 5:42 
AnswerRe: If this leaves open WinWord.exe processes behind Pin
hjy121013-Nov-12 5:20
memberhjy121013-Nov-12 5:20 
Questionwhat are the interop.office,interop.word and interop.VBIDE Pin
pmore5915-Dec-11 20:46
memberpmore5915-Dec-11 20:46 
QuestionNot able to open other Word documents when I open a document with Windowrd Control Pin
Murali Krishna Nakkella12-Oct-11 3:33
memberMurali Krishna Nakkella12-Oct-11 3:33 
GeneralMy vote of 5 Pin
moslemB12-Sep-11 5:49
membermoslemB12-Sep-11 5:49 
QuestionPlease Help Pin
Mahendan2410-May-11 18:19
memberMahendan2410-May-11 18:19 
GeneralWinwordcontrol in VS 2005 Pin
Cesar Almeida24-Jan-11 8:16
memberCesar Almeida24-Jan-11 8:16 
GeneralRe: Winwordcontrol in VS 2005 Pin
dlz114-Aug-12 4:01
memberdlz114-Aug-12 4:01 
GeneralDraw table in Extended richTextbox Pin
abhhy0923-Jan-11 19:28
memberabhhy0923-Jan-11 19:28 
GeneralThe rpc server is not available Pin
Beni Sec2-Jan-11 10:58
memberBeni Sec2-Jan-11 10:58 
GeneralRe: The rpc server is not available Pin
weilidai200124-Jan-11 2:39
memberweilidai200124-Jan-11 2:39 
GeneralRe: The rpc server is not available Pin
bramazzotti3-Nov-11 4:44
memberbramazzotti3-Nov-11 4:44 
GeneralRe: The rpc server is not available Pin
dlz114-Aug-12 3:58
memberdlz114-Aug-12 3:58 
GeneralRe: The rpc server is not available Pin
dlz114-Aug-12 4:32
memberdlz114-Aug-12 4:32 
Generalissue working in Vista Pin
prit_mahudha30-Sep-10 23:09
memberprit_mahudha30-Sep-10 23:09 
Questionword2007 hide ribbon? Pin
Member 724777618-Aug-10 1:27
memberMember 724777618-Aug-10 1:27 
AnswerRe: word2007 hide ribbon? Pin
lifengxiaorong19-Jan-11 21:30
memberlifengxiaorong19-Jan-11 21:30 
Generalinsert a selected text in WinWordControl Pin
david_3434-Jul-10 21:42
memberdavid_3434-Jul-10 21:42 
GeneralRe: insert a selected text in WinWordControl Pin
gery1282-Aug-10 23:47
membergery1282-Aug-10 23:47 
Generalin WinWordControl Pin
david_3434-Jul-10 21:25
memberdavid_3434-Jul-10 21:25 
QuestionHow to disable all toolbar Pin
csrangianges3-Jun-10 2:20
membercsrangianges3-Jun-10 2:20 
Questionwhat is the handleName for Excel & Powerpoint? Pin
gery12812-May-10 1:56
membergery12812-May-10 1:56 
AnswerRe: what is the handleName for Excel & Powerpoint? Pin
gery12812-May-10 19:41
membergery12812-May-10 19:41 
GeneralRe: what is the handleName for Excel & Powerpoint? Pin
gery1282-Aug-10 23:48
membergery1282-Aug-10 23:48 
QuestionSimilar control for Excel & Powerpoint Pin
gery12810-May-10 2:45
membergery12810-May-10 2:45 
AnswerRe: Similar control for Excel & Powerpoint Pin
yltsa10-Aug-11 5:07
memberyltsa10-Aug-11 5:07 
Generalwinwordcontrol.dll without Office Pin
rarobbi3-Dec-09 5:24
memberrarobbi3-Dec-09 5:24 
GeneralRe: winwordcontrol.dll without Office Pin
Rick Pingry30-Dec-09 14:41
memberRick Pingry30-Dec-09 14:41 
Questionlost form control after enter word doc. Pin
Member 16519299-Sep-09 17:24
memberMember 16519299-Sep-09 17:24 
AnswerRe: lost form control after enter word doc. Pin
Rick Pingry30-Dec-09 14:49
memberRick Pingry30-Dec-09 14:49 
GeneralStrange Fixes to Strange Problem Pin
Member 361548728-May-09 16:23
memberMember 361548728-May-09 16:23 
GeneralRe: Strange Fixes to Strange Problem Pin
weilidai200124-Jan-11 2:45
memberweilidai200124-Jan-11 2:45 
GeneralRe: Strange Fixes to Strange Problem Pin
weilidai200124-Jan-11 2:51
memberweilidai200124-Jan-11 2:51 
GeneralThanks! [modified] Pin
Member #568413511-Mar-09 3:24
memberMember #568413511-Mar-09 3:24 
GeneralHtml Pin
Member 387334311-Mar-09 1:22
memberMember 387334311-Mar-09 1:22 
GeneralIt doesn't work for Word 2003 Pin
felixdisc3-Mar-09 7:02
memberfelixdisc3-Mar-09 7:02 
GeneralRe: It doesn't work for Word 2003 Pin
felixdisc4-Mar-09 6:55
memberfelixdisc4-Mar-09 6:55 
GeneralAnother approach using standard .NET Webbrowser component Pin
Howard Jachter13-Jan-09 13:59
memberHoward Jachter13-Jan-09 13:59 
GeneralRe: Another approach using standard .NET Webbrowser component Pin
Ben Robbins4-Feb-09 21:27
memberBen Robbins4-Feb-09 21:27 
GeneralRe: Another approach using standard .NET Webbrowser component Pin
Howard Jachter4-Feb-09 21:53
memberHoward Jachter4-Feb-09 21:53 
GeneralRe: Another approach using standard .NET Webbrowser component Pin
Howard Jachter4-Feb-09 22:02
memberHoward Jachter4-Feb-09 22:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 29 Jan 2003
Article Copyright 2003 by Matthias Hänel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid