Microsoft Word Documents from ASP.NET

xiaoxiao2021-03-06  52

Microsoft Word Documents from ASNETBY Michela Creating and Opening Microsoft Word Documents from ASP.NET?

Download Demo Project - 313 KB

Introduction

. This is the asp.net.

Background

Automation is a process that allows applications that are written in languages ​​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 to documents, mail merge, and formatting documents. With Word and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using automation. Word exposes this programmatic functionality through an 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 is an Application object, a Document object, and a Paragraph object, 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 on References-> Add Reference. Click on the COM tab and look for the Microsoft Word 10.0 Object Library. Click SELECT AND OK.

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:

Word.ApplicationClass OwordApp = New Word.ApplicationClass ();

You can call the interesting methods and properties that Microsoft Word provides to you to manipulate documents 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:. Choose Record New Macro from the Macro option on the Tools menu and execute the task you're interested in. Choose Stop Recording from the Macro option on the Tools menu. Once you are done recording, choose Macros from the Macro option on the Tools menu, Select The Macro You Recorded, The Click Edit.

..............................

For Example To Open An Existing File and Append Some Text:

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 ready,

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;

Or to Open A New Document and Save IT:

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.saves ("c: //myfile.doc");

OWordApp.Application.quit (Ref missing, ref missing;

In C #, the Word Document class's Open method signature is defined as Open (ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object , ref object, ref object, ref object). What this means is that in C # the Open method 15 required arguments, and each argument must be preceded with the ref keyword and each argument must be of type object takes. Since the first argument is A File Name, Normally A String Value In Visual Basic. Net, We Must Declare A Variable of Type Object That Holds The C # String Value, Hence The Code:

Object filename = "c: //database/test.doc";

Although we only need to use the first argument in the Open method, remember that C # does not allow optional arguments, so we provide the final 14 arguments as variables of type object that hold values ​​of System.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 Through You Documents WITH LES 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 as the following: 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:

Object Obookmark = "MyBookmark";

OwordDoc.bookmarks.Item (Ref Obookmark) .range.text = "Some text Here";

Another advantage to using a template is this you can create and store formatting styles That You wish to Apply At Run Time, As Follows:

Object ostylename = "mystyle";

OwordDoc.bookmarks.Item (Ref Obookmark) .Range.set_style (Ref OstyleName);

Using the class ccWordApp

The project contains a file: CCWordApp.cs I did not want to write every time all the code necessary to insert text, open a file, etc ... So I decided to write a class CCWordApp that wraps the most important function.. This is a brief description of the class and its functions.

Public class ccWordApp

{

// it's a reason to the com object of Microsoft Word Application

Private word.Applicationclass OwordApplic;

// it's a reason to the document in use

Private word.document OwordDoc;

// Activate the interface with the com object of Microsoft Word

Public ccWordApp ();

// Open an existing file or open a new file based on A Template

Public void open (String strfilename);

// Open a new documentpublic void open ();

// deActivate the interface with the com object of Microsoft Word

Public vid Quit ();

// save the document

Public void save ();

// save the document with a new name as html document

Public void saveas (string strfilename);

// Save the Document in Html Format

Public void saveashtml (String strfilename);

// Insert Text

Public void inserttext (String Strtext);

// Insert Line Break

Public void insertlinebreak ();

// INSERT MULTIPLE LINE BREAK

Public void insertlinebreak (int nline);

// set the paragraph alignment

// Possible Values ​​of StrtYPE: "Centre", "Right", "Left", "Justify"

Public void setAlignment (String Strtype);

// set the font style

// Possible Values ​​of StrtYPE: "Bold", "Italic," underlined "

Public void setfont (string strtype);

// disable all the style

Public void setfont ();

// set the font name

Public void setFontName (String start ";

// set the font Dimension

Public void setfontsize (int nsize);

// INSERT A Page Break

Public void INSERTPAGEBREAK ();

// Go to a predefined bookmark

Public void gotobookmark (String strbookmarkname);

// Go to the end of document

Public void gotothend ();

// go to the beginning beginning of document

Public void gotothebeginning ();

So the code to open an existing file will be:

CCWORDAPP TEST;

Test = new ccWordApp ();

Test.open ("C: //Database/test.doc");

Test.insertText ("this is the text");

Test.insertlineBreak;

Test.save ();

Test.quit ();

Details

The Demo Project Contains:

CCWordApp.cs - the class CreateDocModel.aspx: an example of building a new document based on a template and the use of bookmarks CreateNewDoc.aspx: an example of building a new document and inserting some text ModifyDocument.aspx: an example of opening an existing document and appending some text at the end template / template1.dot: an example of a template (used in CreateDocModel.aspx) Keep in mind that the directory, in which you save the files, must be writeable Please check the Web.. CONFIG to Change the path.

References

Microsoft Word Objects Converting Microsoft Office VBA Macros to Visual Basic .NET and C # HOWTO: Automate Microsoft Word to Perform a Mail Merge from Visual Basic .NET A Primer to the Office XP Primary Interop Assemblies HOWTO: Find and Use Office Object Model Documentation Creating and Opening Microsoft Word Documents from .NET USING C #

Michela

Click Here to View Michela's Online Profile.? Original: http://www.codeproject.com/aspnet/wordapplication.asp? Msg = 916345

转载请注明原文地址:https://www.9cbs.com/read-116607.html

New Post(0)