5,427,303 members and growing! (19,941 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Word Control for .NET

By Matthias Hänel

Writing a control to use MS-Word in your own Application like a windows-form.
C#, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 28 Jan 2003
Updated: 28 Jan 2003
Views: 248,321
Bookmarked: 104 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
35 votes for this Article.
Popularity: 6.73 Rating: 4.36 out of 5
2 votes, 5.7%
1
1 vote, 2.9%
2
0 votes, 0.0%
3
7 votes, 20.0%
4
25 votes, 71.4%
5

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Matthias Hänel


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
Occupation: CEO
Location: Germany Germany

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 168 (Total in Forum: 168) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionCan't Find Winwirdcontroll in the listmemberJimilybibilybob9:57 19 Apr '08  
QuestionCommandBars Errormemberswje0:19 8 Apr '08  
GeneralVC++.NET 2003 and word [modified]memberminad_7861:40 11 Sep '07  
GeneralThe rpc server is not availablememberSalman Sheikh3:33 4 Sep '07  
QuestionWinWordControl and Landscapememberrandy12004:03 31 Aug '07  
QuestionTaskpane and event handler probleme need some helpmemberspeedyc861:44 16 Aug '07  
Generalcheck spelling and grammar for the word documentmembervinh_2001pt20:10 22 Jul '07  
QuestionCan we load another application that is not MS product into this form?membertuine200612:09 19 Apr '07  
QuestionProblem with opening/Saving a DocumentmemberRakesh B Singh3:21 23 Feb '07  
AnswerRe: Problem with opening/Saving a Document [modified]membernpkinh0:15 18 May '07  
AnswerRe: Problem with opening/Saving a Documentmemberbidalah11:30 16 Aug '07  
AnswerRe: Problem with opening/Saving a Documentmemberaerradi18:14 18 Aug '07  
GeneralError While Processing another word document in running state of this controlmembersdivya15:49 12 Jan '07  
GeneralHate to say this....memberRoss Holder13:49 6 Dec '06  
GeneralRe: Hate to say this....memberbidalah11:40 16 Aug '07  
GeneralMS Projectmemberjamesrgoodwin5:46 6 Dec '06  
GeneralRun more than one instancememberItsKodali20:21 4 Dec '06  
Questionhow to set readonly property for winwordcontrolmembertanthanhduy18:43 17 Oct '06  
GeneralHow to enable documentmembertanthanhduy18:16 15 Oct '06  
AnswerRe: How to enable documentmemberRakesh B Singh3:27 23 Feb '07  
QuestionFocus and Z ordermemberstealth24111:05 5 Oct '06  
GeneralREAD ONLYmemberNikeshM0:16 23 Jul '06  
GeneralHow do I open documents?membermkamioner13:33 20 Jul '06  
GeneralProblem...membersam_g12:06 23 Mar '06  
General