Http://www.codeproject.com/KB/aspnet/wordapplication.aspx
Introduction
This article is being written in response of the need of building Microsoft Word document in an ASP. NET project. This article demonstrates how to create and modify document using Microsoft Word with ASP. NET.
Background
Automation is a process that allows applications that are written in versions ages such as Visual Basic. net or C # to programmatically control other applications. automation to Word allows you to perform actions such as creating new documents, adding text
Documents, mail merge, and formatting documents. with word and other Microsoft Office applications, always Ally all of the actions that you can perform manually through the user interface can also be saved med programmatically by using automation. word exposes
This programmatic functionality through
Object Model. The object model is a collection of classes and methods that serve as counterparts to the logical components of word. For example, there isApplicationObject,
DocumentObject, andParagraphObject, each of which contain the functionality of those components in word.
The project
The first step in manipulating word in. NET is that you'll need to add a com reference to your project by Right clicking in the Solution Explorer onReferences-> Add reference. Click on the com tab and look for the Microsoft Word 10.0 Object Library.
ClickSelectAndOK.
This will automatically place an assembly in your application directory that wraps com access to word.
Now you can instantiate an instance of a word application:
Collapse | copy
Code
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
You can call the interesting methods and properties that Microsoft Word provides to you to manipulate statements in word. the best way to learn how to navigate the object models of Word, Excel, and PowerPoint is to use the macro recorder in these office applications:
- ChooseRecord new macroFromMacroOption onToolsMenu and execute the task you're interested in.
- ChooseStop recordingFromMacroOption onToolsMenu.
- Once you are done recording, chooseMacrosFromMacroOption onToolsMenu, select the macro you recorded, then click
Edit.
This takes you to the generated VBA code that accomplishes the task you recorded. Keep in mind that the recorded Macro willNotBe the best possible code in most cases, but it provides a quick and usable example.
For example to open an existing file and append some text:
Collapse | copy
Code
object fileName = "c:\\database\\test.doc";object readOnly = false;object isVisible = true;object missing = System.Reflection.Missing.Value;Word.ApplicationClass oWordApp = new Word.ApplicationClass();Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing,ref missing,ref missing);oWordDoc.Activate();oWordApp.Selection.TypeText("This is the text");oWordApp.Selection.TypeParagraph();oWordDoc.Save();oWordApp.Application.Quit(ref missing, ref missing, ref missing);
Or to open a new document and save it:
Collapse | copy
Code
Word.ApplicationClass oWordApp = new Word.ApplicationClass();Word.Document oWordDoc = oWordApp.Documents.Add(ref missing, ref missing,ref missing, ref missing);oWordDoc.Activate();oWordApp.Selection.TypeText("This is the text");oWordApp.Selection.TypeParagraph();oWordDoc.SaveAs("c:\\myfile.doc");oWordApp.Application.Quit(ref missing, ref missing, ref missing);
In C #, the wordDocumentClass'sOpenMethod signature is definedOpen(ref
object, ref
object,ref object,
ref object,
refobject, ref
object, ref
object,ref object,
ref object,
refobject, ref
object, ref
object,ref object,
ref object,
refobject). What this means is that in C #OpenMethod takes 15 required arguments, and each argument must be preceded withref
Keyword and each argument must be of Typeobject. Since the first argument is a file name, normallyStringValue in Visual Basic. net, we must
Declare a variable of TypeobjectThat holds the C #stringValue, hence the Code:
Collapse | copy
Code
object fileName = "c:\\database\\test.doc";
Although we only need to use the first argument inOpenMethod, remember that C # does not allow optional arguments, so we provide the final 14 arguments as variables of Typeobject
That hold valuesSystem.Reflection.Missing.Value
Use a template
If you are using automation to build documents that are all in a common format, you can benefit from starting the process with a new document that is based on a preformatted template. using a template with your word automation client has two significant
Advantages over building a document from nothing:
- You can have greater control over the formatting and placement of objects throughout your documents.
- You can build your documents ents with less code.
By using a template, You can fine-tune the placement of tables, paragraphs, and other objects within the document, as well as include formatting on those objects. by using automation, you can create a new document based on your template with Code such
The following:
Collapse | copy
Code
Word.ApplicationClass oWordApp = new Word.ApplicationClass();object oTemplate = "c:\\MyTemplate.dot";oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref Missing,ref Missing, ref Missing);
In your template, you can define bookmarks so that your automation client can fill in variable text at a specific location in the document, as follows:
Collapse | copy
Code
object oBookMark = "MyBookmark";oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
Another advantage to using a template is that you can create and store formatting styles that you wish to apply at run time, as follows:
Collapse | copy
Code
object oStyleName = "MyStyle";oWordDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);