
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;
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:
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.
|
|
 |
 | Excel in dotnet (Please help me) nishat | 1:41 17 Jan '10 |
|
 |
Dear sir, I am doing same thing with excel 2007. Excel is loaded in window form application but Workbook of application is disabled. I mean i can not chnage the workbook of excel application.
Regards,
Please help me.
|
|
|
|
 |
|
|
 |
 | how to forbidden the users to edit the loaded MS WORD' content love19861018 | 5:18 2 Jan '10 |
|
 |
who can help me! I am Kneeling for the replying in the snow
|
|
|
|
 |
 | can you sent the source to me love19861018 | 5:06 2 Jan '10 |
|
 |
I can not download the source,can you sent to me ,thank you ,my Email:love19861018@126.com 
|
|
|
|
 |
 | Thanks a lot asawant | 0:30 24 Dec '09 |
|
 |
Thanks a lot Anup for the sample code. I am facing some problems as I am calling this dialog box (with word control ) by clicking the cutomize menu in the word. In short my word application is running in background. The dialog box is having tree at left pane and on selection of node I am displaying data in word using the mentioned control. The system is getting hang. Is it not possible that one word application can call the word control again? Help me it is very urgent.
|
|
|
|
 |
 | Thanks a lot rostamiani | 21:49 6 Nov '09 |
|
 |
Thanks a lot I was searching this solution for several weeks
Thanks a lot Persian Thanks : خدا پدر و مادرتو بیامرزه
|
|
|
|
 |
 | problem when unhide Menubar Kernel1001 | 20:24 30 Oct '09 |
|
 |
Hi, Anup Shinde,
Your program is quite good, but i want to ask. If we needn't hide menubar (maximize, minimize button), do we have another way to disable them?
|
|
|
|
 |
 | How to open more word document in one container ? josephlee0717 | 18:59 13 Apr '09 |
|
 |
Hi, I made a user control container, and put a word control on it. Every time, I create a new tab open a word file. But, only one file can be show in tab. How to open more word document in one container ?
|
|
|
|
 |
 | Problem with SAVE (not save as) document!!! Hero from H.U.T | 19:09 9 Apr '09 |
|
 |
First, I want to thank for the helpful solution. It inherits a previous solution with MS Word 2000
But, It is so pity when a small problem when I want to save document (not save as). The saving Function, Method of the WinWordControl (//WinWordControl.document.Save()//)in the solutions doesn't work. When I fire the save "document button", It opens a Save As dialog in spite of saving document to itself. It seems the document is opened in another thread (maybe the thread to integrate Word Document to Window Form), not only is ApplicationClass Thread
So, I hope for a new solution for this problem. I'm very appreciate for incoming replies.
Thank you so much!! 
|
|
|
|
 |
 | How to find the cursor text spprofile | 20:43 30 Mar '09 |
|
 |
Could you please explain me how do i get the cursor text using this winword control
Please help me on this.Thanks in advance.
|
|
|
|
 |
 | Excel sheet is opening seperately Member 4724512 | 1: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
|
|
|
|
 |
 | Windows is searching for ConvertDoc.exe bobk544 | 5:50 12 Jan '09 |
|
 |
Hello, thanks for the very helpful article! When i run the program and click "Load Doc" i keep getting a window popup that says: Windows is searching for ConvertDoc.exe followed by "The device or network connection that the shortcut Convert Doc.lnk ... Thanks for any assitance with this! BobK
bk
|
|
|
|
 |
 | ActiveX? reza_arab | 19:29 23 Nov '08 |
|
 |
thanks for your great article, please illustrate few about converting this control to activex and use this in web application. thanks again. 
|
|
|
|
 |
 | hi kazim bhai | 7:39 29 Oct '08 |
|
 |
How can i use the saveas in my program
What parameters do I need?
Your program is quiet good
Thanks in advance
Kazim
|
|
|
|
 |
 | Disable Close [X] on menu bar Member 1628719 | 18:17 22 Sep '08 |
|
 |
Hi,
I don't want to hide whole menu bar. i just want to hide File, Help etc few options in menu bar. So i used the code suggested ,
Dim c As Office.CommandBarPopup c = CType(wd.ActiveWindow.Application.CommandBars("Menu Bar").Controls (1), Office.CommandBarPopup) c.Enabled = False
This is working fine. But [X] button on menu bar is still active. how to disable that? This [X] button is for document.
[X] for whole word application is already disabled. But if you reset toolbar it will be active. How to avoide this?
Thanks in advance.
|
|
|
|
 |
 | opening new word document Member 1628719 | 21:50 14 Sep '08 |
|
 |
Hi,
Very nice and useful article.
How to open new document if i don't want to open any existing one?
Thanks in advance.
|
|
|
|
 |
 | Any way we can macros in the word file to run here? Member 4209563 | 3:53 18 Jul '08 |
|
 |
Hi anup, request some help here! Is there any way, to enable macros in the word session within the form? I am opening a word file which has a few macros. But I guess the security feature disables all the macros.
Venkat
|
|
|
|
 |
 | Cursor Position Problem Goje06 | 2:58 14 Jul '08 |
|
 |
Hello Anup
This is realy great control.
This control slove all my problem about word automization.
But still i am facing one problem and that is
- Corsor Position.
when we open the document into winword control this is not showing any cursor on the document, when user click on document then they can able to see the cursor.
One more thing when they press Alt + Tab keys for changr the window
and again select the winword control again can not see cursor when they left it.
again they have to click on document then it shows the curson
Due to this resion this control is not so much Key Board Friendely.
Please help me.
Regards
-Gaurav
|
|
|
|
 |
 | Good [Help Me with Excel container Like This Sachin Dubey | 22:46 17 Jun '08 |
|
 |
Help Me with Excel container Like This
modified on Wednesday, June 18, 2008 4:56 AM
|
|
|
|
 |
 | Well Done JordanCode | 9:34 3 Jun '08 |
|
 |
Hey There
Excellent Coding!
Thanx for the contribution!
Jordan
|
|
|
|
 |
 | disable shortcut keys Member 2339270 | 1:16 2 Jun '08 |
|
 |
even though i'm able to hide the new, open, save etc buttons... i can still call their functionality using the shortcut keys e.g, Cltr+O will display the open file dialog... how can i disable these shortcut keys? Any ideas/hints ???
|
|
|
|
 |
 | Can we use it For Web Applications? AmitChampaneri | 0:52 15 May '08 |
|
 |
Its great article,can we use it for web applications creating its web user control?
|
|
|
|
 |
|
 |
Not until u make something like an ActiveX...and user must be using MSWord... you will have to figure out how... This was made for older version of MSOffice...new versions of MSOffice must be having something better ..
|
|
|
|
 |
 | Excellent!
Muammar©
| 1:18 5 May '08 |
|
 |
Really excellent, Thanks mate!
All generalizations are wrong, including this one! (\ /) (O.o) (><)
|
|
|
|
 |
 | deactivate or hide save button of this control... in VB.NET Jats_4ru | 2:25 17 Apr '08 |
|
 |
Mr.Anup,
I actually want to use this control in my project.
So, i am looking for some specific feature in it. Where i can open a word file with in my app, once the user changes the file and press save button i want to save it to a different location as per the future need.
Now i am able to open the file in the app and make changes, but when the user presses the ssve button it actuallly saves at the same location i mean it overwrites the document. Here i want it to take a location as input from code and save the file there. As i want the control to be handled by me.
Or else if i can deactivate New, Open and Save buttons. As open and new are already deactivated in the control is there a way where i can deactiviate (or) hide the save buuton of the control.
Second question is how to do it in VB.NET as i am using it in VB.NET
Any hlp or idea will be great....
Thank you
Jats
|
|
|
|
 |
|
|