Click here to Skip to main content
Licence 
First Posted 30 Sep 2005
Views 219,366
Downloads 7,291
Bookmarked 213 times

Integrating Microsoft Word in your .NET applications

By Anup Shinde | 30 Sep 2005
This article explains how you can integrate Microsoft Word in your .NET applications.
2 votes, 3.8%
1

2
2 votes, 3.8%
3
7 votes, 13.5%
4
41 votes, 78.8%
5
4.89/5 - 52 votes
4 removed
μ 4.67, σa 1.55 [?]

Introduction

This article explains how we can use Microsoft Word in our .NET application. This will enable you to use your favorite word-processor within your .NET application.

Background

I had been working on migrating and improving different transcription workflow applications (medical and legal) in .NET. The biggest problem that these transcriptionists faced while transcribing was that of the editor. All of them used an HTML editor which is a part of the Windows Form application. In the period of 4-5 years of its usage, the clients started using MS-Word. Therefore they wanted us to send Word documents as output, instead of the HTML documents sent earlier. The workflow was changed by doing this manually. We wanted to have a Word control similar to the HTML editor to become a part of the application. Unfortunately, there is no such way to do that.

While searching at Google, I came across a project which showed the trick of making any window a part of your application. This article is based on the article posted at CodeProject. Just when I was writing this article, I found another article suggesting the same trick.

Using the code

I would highly recommend reading the base-article mentioned in the Background section.

When we contain a window within our application, we do not want the window to be allowed to close through its Close (X) button. Similarly, we do not want the Minimize, Maximize and Restore buttons to work.

One way to solve this problem is to hide the title bar. This can be done by using the SystemInformationn class, as shown below:

int borderWidth = SystemInformation.Border3DSize.Width;
int borderHeight = SystemInformation.Border3DSize.Height;
int captionHeight = SystemInformation.CaptionHeight;
int statusHeight = SystemInformation.ToolWindowCaptionHeight;

// Move the window in the container so that the 
// container borders hide the window's title bar.
MoveWindow(
  wordWnd, 
  -2*borderWidth,
  -2*borderHeight - captionHeight, 
  this.Bounds.Width + 4*borderWidth, 
  this.Bounds.Height + captionHeight + 4*borderHeight + statusHeight,
  true);

But what would happen if the user presses "Alt+Spacebar"? This will open up the System menu for the user, where the user can select Restore, Minimize, Close, etc. We don't want even this to happen. We can disable this System menu using the Windows API. This is shown below:

// We want to remove the system menu also. 
// The title bar is not visible, but we want to avoid 
// accidental minimize, maximize, etc ..by 
// disabling the system menu (Alt+Space) 
int hMenu = GetSystemMenu(wordWnd,false);

if(hMenu>0)    
{ 
  int menuItemCount    = GetMenuItemCount(hMenu); 
  RemoveMenu(hMenu, menuItemCount - 1, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 2, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 3, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 4, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 5, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 6, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 7, 
                               MF_REMOVE    | MF_BYPOSITION); 
  RemoveMenu(hMenu, menuItemCount - 8, 
                               MF_REMOVE    | MF_BYPOSITION);
  DrawMenuBar(wordWnd);
}

You may want to have only a few command bars in the MS-Word window. E.g. you may not want the "Menu Bar" (which is also a command bar in MS-Word). This is shown below:

[Editor comment: Line breaks used to avoid scrolling.]

objWinWordControl.document.ActiveWindow.Application.
                  CommandBars["Menu Bar"].Enabled=false;

The "Standard" command bar in MS-Word has the "New" and "Open" buttons. We want to disable these buttons also. This can be done as shown below:

[Editor comment: Line breaks used to avoid scrolling.]

wd.ActiveWindow.Application.CommandBars["Standard"].
                              Controls[1].Enabled=false;
wd.ActiveWindow.Application.CommandBars["Standard"].
                              Controls[2].Enabled=false;

In some cases, you will need to have the "Menu Bar", but without all the sub-menu options. You can disable those options by using the CommandBarPopUp class:

Office.CommandBarPopup c;
c = (Office.CommandBarPopup)wd.ActiveWindow
                         .Application
                         .CommandBars["Menu Bar"].Controls[0];
c.Controls["Close Window"].Enabled=false;

The attached code

The attached code is part of a larger transcription workflow. It is developed using .NET 1.1 Framework and MS-Word 2000.

In most transcription applications, you have a scenario like "Transcription --> QA1 --> QA2.....". In such cases, you will have to give an error-marking facility to the QAs. I have just shown a very basic demo. Say, when a transcript goes from QA1 to QA2, the errors marked by QA2 should not be shown. Therefore, we need to remove error-marking before displaying the transcript to QA2.

We need to have specific kinds of templates for different clients of the transcription division. These templates can be pre-configured and used to create new documents (through the "LoadDocument" method). An example of this is shown through the "Load Document" button.

In most cases, you will require that a part of the document being transcribed be automatically filled up. E.g. the client's ID, current date, record number, etc. This kind of information comes from the database, therefore can be automatically filled up. I have accomplished this using "Bookmarks" in the Word document. A sample can be seen using the "Sample.dot" file.

You can do much more than what is shown in the attached code. Please find more about Microsoft Word Automation and Windows APIs at MSDN.

Enhancements

While this application only integrates (contains) MS-Word in a .NET application, it was quite interesting to do so with other applications as well. We did that with an audio player and found that the concept of containing windows was really useful the second time too.

Please feel free to send me your suggestions here or email me at anup@micromacs.com. The article's original page can be found here.

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

Anup Shinde

Web Developer

India India

Member
Microsoft Certified Solution Developer (.NET) working with a software development company in India.
 
Achieved Masters in Computer Applications and Bachelor degree in Electronics
 
Areas of interest: AI..specifically Genetic Algorithms, Machine learning, automation engineering, user interface designs, human computer interaction, biotechnology, nanotechnology, networking technologies (computers as well as social)....anything where my brain can do some real research Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWill not compile with Word 2007 Pinmemberstevehoang6:54 17 Jan '12  
QuestionActive sheet Pinmemberkienhv_871:19 13 Oct '11  
Bugfacing starange issue PinmemberMurali Krishna Nakkella4:37 12 Oct '11  
QuestionDisplay excel in winform with c# Pinmemberkienhv_8723:53 5 Oct '11  
GeneralI am tryin g to Integrate MS Excel 2007 instead of MS word.... Pinmemberanandchettiar9:25 13 Aug '10  
GeneralRe: I am tryin g to Integrate MS Excel 2007 instead of MS word.... Pinmembergery1280:23 30 Nov '10  
GeneralRe: I am tryin g to Integrate MS Excel 2007 instead of MS word.... Pinmemberdairyman10my22:45 1 Jul '11  
Generalinterop dll PinmemberMember 37605873:17 8 Jun '10  
GeneralGreat control, but I need a small improvement PinmemberMashinovodja23:58 9 May '10  
GeneralExcel in dotnet (Please help me) Pinmembernishat1:41 17 Jan '10  
AnswerRe: Excel in dotnet (Please help me) Pinmemberdereklk20:25 24 Jan '10  
Questionhow to forbidden the users to edit the loaded MS WORD' content Pinmemberlove198610185:18 2 Jan '10  
Questioncan you sent the source to me Pinmemberlove198610185:06 2 Jan '10  
GeneralThanks a lot Pinmemberasawant0:30 24 Dec '09  
GeneralThanks a lot Pinmemberrostamiani21:49 6 Nov '09  
Generalproblem when unhide Menubar PinmemberKernel100120:24 30 Oct '09  
QuestionHow to open more word document in one container ? Pinmemberjosephlee071718:59 13 Apr '09  
QuestionProblem with SAVE (not save as) document!!! PinmemberHero from H.U.T19:09 9 Apr '09  
QuestionHow to find the cursor text Pinmemberspprofile20:43 30 Mar '09  
GeneralExcel sheet is opening seperately PinmemberMember 47245121:42 15 Jan '09  
Excel sheet is opening seperately but I want that with in the window form.
 
I have given navigation option false as follows in EmbeddedExcel.ExcelWrapper.OpenFile() method
 
this.WebBrowserExcel.Navigate(filename,false);
 
Please help me.
 
Thanks & Regards,
Nishant
GeneralWindows is searching for ConvertDoc.exe Pinmemberbobk5445:50 12 Jan '09  
QuestionActiveX? Pinmemberreza_arab19:29 23 Nov '08  
Questionhi Pinmemberkazim bhai7:39 29 Oct '08  
GeneralDisable Close [X] on menu bar PinmemberMember 162871918:17 22 Sep '08  
Questionopening new word document PinmemberMember 162871921:50 14 Sep '08  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 30 Sep 2005
Article Copyright 2005 by Anup Shinde
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid