Click here to Skip to main content
12,510,238 members (51,455 online)
Click here to Skip to main content
Add your own
alternative version

Stats

309.3K views
14.4K downloads
262 bookmarked
Posted

Integrating Microsoft Word in your .NET applications

, 30 Sep 2005
Rate this:
Please Sign up or sign in to vote.
This article explains how you can integrate Microsoft Word in your .NET applications.
This is an old version of the currently published article.

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

Share

About the Author


You may also be interested in...

Pro
Pro

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
Questionfocus change Pin
revilo729-Jan-13 9:23
memberrevilo729-Jan-13 9:23 
QuestionAdd New word docs Pin
mayuryewale6-Jan-13 22:56
membermayuryewale6-Jan-13 22:56 
QuestionSaving the document open SaveAs Dialoguebox Pin
Pritesh Aryan20-Nov-12 17:52
memberPritesh Aryan20-Nov-12 17:52 
AnswerRe: Saving the document open SaveAs Dialoguebox Pin
Usman Mahmood6-Jan-13 5:16
memberUsman Mahmood6-Jan-13 5:16 
GeneralRe: Saving the document open SaveAs Dialoguebox Pin
Pritesh Aryan9-Jan-13 2:21
memberPritesh Aryan9-Jan-13 2:21 
GeneralMy vote of 5 Pin
Pritesh Aryan30-Oct-12 2:52
memberPritesh Aryan30-Oct-12 2:52 
QuestionHow to Integrate Word in WebForm asp.net C# Pin
Xuan Dung Phu Yen25-Aug-12 23:10
memberXuan Dung Phu Yen25-Aug-12 23:10 
AnswerRe: How to Integrate Word in WebForm asp.net C# Pin
Anup Shinde26-Aug-12 9:28
memberAnup Shinde26-Aug-12 9:28 
GeneralRe: How to Integrate Word in WebForm asp.net C# Pin
Xuan Dung Phu Yen26-Aug-12 13:52
memberXuan Dung Phu Yen26-Aug-12 13:52 
QuestionHow to get the line number in current mouse position using winWordControl with in the form? Pin
udayakumard9-Aug-12 2:16
memberudayakumard9-Aug-12 2:16 
GeneralMy vote of 4 Pin
Muhammad Shahid Farooq8-Aug-12 21:10
memberMuhammad Shahid Farooq8-Aug-12 21:10 
Questionsave as or save Pin
sgrysoft8-May-12 3:45
membersgrysoft8-May-12 3:45 
GeneralRe: save as or save Pin
Pritesh Aryan20-Nov-12 17:47
memberPritesh Aryan20-Nov-12 17:47 
QuestionWill not compile with Word 2007 Pin
stevehoang17-Jan-12 5:54
memberstevehoang17-Jan-12 5:54 
QuestionActive sheet Pin
kienhv_8713-Oct-11 0:19
memberkienhv_8713-Oct-11 0:19 
Bugfacing starange issue Pin
Murali Krishna Nakkella12-Oct-11 3:37
memberMurali Krishna Nakkella12-Oct-11 3:37 
QuestionDisplay excel in winform with c# Pin
kienhv_875-Oct-11 22:53
memberkienhv_875-Oct-11 22:53 
GeneralI am tryin g to Integrate MS Excel 2007 instead of MS word.... Pin
anandchettiar13-Aug-10 8:25
memberanandchettiar13-Aug-10 8:25 
GeneralRe: I am tryin g to Integrate MS Excel 2007 instead of MS word.... Pin
gery12829-Nov-10 23:23
membergery12829-Nov-10 23:23 
GeneralRe: I am tryin g to Integrate MS Excel 2007 instead of MS word.... Pin
dairyman10my1-Jul-11 21:45
memberdairyman10my1-Jul-11 21:45 
Generalinterop dll Pin
Member 37605878-Jun-10 2:17
memberMember 37605878-Jun-10 2:17 
GeneralGreat control, but I need a small improvement Pin
Mashinovodja9-May-10 22:58
memberMashinovodja9-May-10 22:58 
GeneralExcel in dotnet (Please help me) Pin
nishat17-Jan-10 0:41
membernishat17-Jan-10 0:41 
AnswerRe: Excel in dotnet (Please help me) Pin
dereklk24-Jan-10 19:25
memberdereklk24-Jan-10 19:25 
Questionhow to forbidden the users to edit the loaded MS WORD' content Pin
love198610182-Jan-10 4:18
memberlove198610182-Jan-10 4:18 
Questioncan you sent the source to me Pin
love198610182-Jan-10 4:06
memberlove198610182-Jan-10 4:06 
GeneralThanks a lot Pin
asawant23-Dec-09 23:30
memberasawant23-Dec-09 23:30 
GeneralThanks a lot Pin
rostamiani6-Nov-09 20:49
memberrostamiani6-Nov-09 20:49 
Generalproblem when unhide Menubar Pin
Kernel100130-Oct-09 19:24
memberKernel100130-Oct-09 19:24 
QuestionHow to open more word document in one container ? Pin
josephlee071713-Apr-09 17:59
memberjosephlee071713-Apr-09 17:59 
QuestionProblem with SAVE (not save as) document!!! Pin
Hero from H.U.T9-Apr-09 18:09
memberHero from H.U.T9-Apr-09 18:09 
QuestionHow to find the cursor text Pin
spprofile30-Mar-09 19:43
memberspprofile30-Mar-09 19:43 
GeneralExcel sheet is opening seperately Pin
Member 472451215-Jan-09 0:42
memberMember 472451215-Jan-09 0:42 
GeneralWindows is searching for ConvertDoc.exe Pin
bobk54412-Jan-09 4:50
memberbobk54412-Jan-09 4:50 
QuestionActiveX? Pin
reza_arab23-Nov-08 18:29
memberreza_arab23-Nov-08 18:29 
Questionhi Pin
kazim bhai29-Oct-08 6:39
memberkazim bhai29-Oct-08 6:39 
GeneralDisable Close [X] on menu bar Pin
Member 162871922-Sep-08 17:17
memberMember 162871922-Sep-08 17:17 
Questionopening new word document Pin
Member 162871914-Sep-08 20:50
memberMember 162871914-Sep-08 20:50 
QuestionAny way we can macros in the word file to run here? Pin
Member 420956318-Jul-08 2:53
memberMember 420956318-Jul-08 2:53 
GeneralCursor Position Problem Pin
Goje0614-Jul-08 1:58
memberGoje0614-Jul-08 1:58 
GeneralGood [Help Me with Excel container Like This Pin
Sachin Dubey17-Jun-08 21:46
memberSachin Dubey17-Jun-08 21:46 
GeneralWell Done Pin
JordanCode3-Jun-08 8:34
memberJordanCode3-Jun-08 8:34 
Generaldisable shortcut keys Pin
Member 23392702-Jun-08 0:16
memberMember 23392702-Jun-08 0:16 
GeneralRe: disable shortcut keys Pin
NarVish11-Apr-10 20:44
memberNarVish11-Apr-10 20:44 
QuestionCan we use it For Web Applications? Pin
AmitChampaneri14-May-08 23:52
memberAmitChampaneri14-May-08 23:52 
AnswerRe: Can we use it For Web Applications? Pin
Anup Shinde15-May-08 3:58
memberAnup Shinde15-May-08 3:58 
GeneralExcellent! Pin
Muammar©5-May-08 0:18
member Muammar© 5-May-08 0:18 
Generaldeactivate or hide save button of this control... in VB.NET Pin
Jats_4ru17-Apr-08 1:25
memberJats_4ru17-Apr-08 1:25 
GeneralRe: deactivate or hide save button of this control... in VB.NET Pin
Member 23392702-Jun-08 0:11
memberMember 23392702-Jun-08 0:11 
Generalintegrating ms word & windows form Pin
medikondu.bhaskar20-Feb-08 17:22
membermedikondu.bhaskar20-Feb-08 17:22 

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.

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