How to: Use Visual C # .NET to make Word automatic new document (from MSDN)

xiaoxiao2021-03-06  75

For Microsoft Visual Basic .NET versions of this article, see

316383.

For the Microsoft Visual Basic 6.0 version of this article, see

313193.

This task content

Summary

Code example usage template reference

Summary This article describes how to create a new document in Word using Visual C # .NET.

Back to top

The code example in this document describes how to complete the following tasks:

Insert a paragraph containing text and format. Browse and modify the different ranges in the document. Insert the table, set the table format and populate the data in the table. Add a chart. To create a new Word document with the automation feature of Visual C # .NET, perform the following steps:

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Under Project Type, click Visual C # item, then click Windows Applications under Templates. FORM1 is created by default. Add a reference to the Microsoft Word object library. To do this, follow these steps:

On the project menu, click Add Reference. On the COM tab, find the Microsoft Word object library, and then click Select. Click OK in the Add Reference dialog to accept your choice. If the system prompts you to generate a package for the selected library, click Yes. On the View menu, select the Toolbox to display the toolbox, then add a button to Form1. Double click on Button1. The code window of the form appears. In the code window, put the following code private void button1_click (Object Sender, System.EventArgs E)

{

}

Replace with: Private Void Button1_Click (Object Sender, System.EventArgs E)

{

Object omissing = system.reflection.Missing.Value;

Object OndofDoc = "// endofdoc"; / * / endofdoc is a predefined bookmark * /

// Start Word and Create a new document.

Word._Application Oword;

Word._document odoc;

OWORD = new word.application ();

Oword.visible = true;

Odoc = Oword.documents.add (Ref Omissing, Ref Omissing,

Ref omissing, ref omissing;

// INSERT A Paragraph at the beginning of the document.

Word.Paragraph Opara1;

Opara1 = odoc.content.paragraphs.add (ref omissing);

Opara1.Range.Text = "Heading 1";

Opara1.Range.Font.Bold = 1;

Opara1.Format.SpaceAfter = 24; // 24 Pt Speacing after paragraph.

Opara1.range.Insertparagraphafter ();

// INSERT A Paragraph at the end of the document.

Word.Paragraph Opara2;

Object orng = odoc.bookmarks.get_item (refooledofdoc) ​​.range; opra2 = odoc.content.paragraphs.add (Ref orng);

Opara2.Range.Text = "Heading 2";

Opara2.Format.SpaceAfter = 6;

Opara2.range.insertparagraphafter ();

// INSERT Another Paragraph.

Word.Paragraph Opara3;

ORNG = odoc.bookmarks.get_item (Ref OndofDoc) .range;

Opara3 = odoc.content.paragraphs.add (ref r);

Opara3.Range.text = "this is a callense of normal text. Now here is a table:";

Opara3.Range.Font.Bold = 0;

Opara3.Format.SpaceAfter = 24;

Opara3.range.insertparagraphafter ();

// INSERT A 3 x 5 Table, Fill It with data, and make the first row

// bold and italic.

Word.Table Otable;

Word.Range Wrdrng = odoc.bookmarks.get_item (Ref OndofDoc) .range;

Otable = odoc.tables.add (WRDRNG, 3, 5, Ref Omissing, Ref Omissing);

Otable.Range.ParagraphFormat.SpaceAfter = 6;

INT R, C;

String strText;

For (r = 1; r <= 3; R )

FOR (c = 1; c <= 5; C )

{

Strtext = "R" R "C" C;

Otable.cell (r, c) .range.text = strText;

}

Otable.Rows [1] .range.Font.bold = 1;

Otable.Rows [1] .range.Font.IAlic = 1;

// Add some text after the table.

Word.Paragraph Opara4;

ORNG = odoc.bookmarks.get_item (Ref OndofDoc) .range;

Opara4 = odoc.content.Paragraphs.Add (Ref orng);

Opara4.range.insertparagraphbefore ();

Opa4.Range.Text = "and here's annother table:";

Opa4.Format.SpaceAfter = 24;

Opara4.range.insertparagraphafter ();

// INSERT A 5 x 2 Table, Fill IT with Data, And Change The Column Widths.

Wrdrng = odoc.bookmarks.get_item (Ref OndofDoc) .range;

Otable = odoc.tables.add (Wrdrng, 5, 2, ref omissing, ref omissing); Otable.Range.ParagraphFormat.SpaceAfter = 6;

For (r = 1; r <= 5; R )

FOR (c = 1; c <= 2; C )

{

Strtext = "R" R "C" C;

Otable.cell (r, c) .range.text = strText;

}

Otable.columns [1] .width = Oword.inchestopoints (2); // Change Width of Column 1 & 2

Otable.columns [2] .width = Oword.inchestopoints (3);

// Keep Inserting Text. When You get to 7 inches from top of the

// Document, INSERT A HARD Page Break.

Object opos;

Double DPOS = Oword.inchestopoints (7);

Odoc.bookmarks.get_item (refoondofdoc) ​​.Range.insertparagraphafter ();

DO

{

Wrdrng = odoc.bookmarks.get_item (Ref OndofDoc) .range;

Wrdrng.ParagraphFormat.SpaceAfter = 6;

Wrdrng.insertafter ("a line of text");

Wrdrng.insertparagraphafter ();

Opos = Wrdrng.get_information

Word.wdinformation.wdvertagePositionRelativeToPage);

}

While (DPOS> = Convert.TODOUBLE (OPOS));

Object ocollapsend = word.wdcollapsedirection.wdcollapsend;

Object Opagebreak = word.wdbreaktype.wdpagebreak;

Wrdrng.collapse (Ref Ocollapseend);

Wrdrng.insertbreak (Ref Opagebreak);

Wrdrng.collapse (Ref Ocollapseend);

Wrdrng.insertafter ("We're now on page 2. here's my chart:");

Wrdrng.insertparagraphafter ();

// INSERT A CHART.

Word.inlineshape Oshape;

Object oclasstype = "msgraph.chart.8";

Wrdrng = odoc.bookmarks.get_item (Ref OndofDoc) .range;

OSHAPE = Wrdrng.inlineshapes.addoleObject (Ref OclasStype, Ref Omissing,

Ref omissing, ref omissing, ref omissing,

Ref omissing, ref omissing;

// Demonstrate Use of late bound ochart and ochartapp objects to // manipulate the chart Object with msgraph.

Object Ochart;

Object OchartApp;

Ochart = OSHAPE.OLEFORMAT.OBJECT;

Ochartapp = Ochart.gettype (). InvokeMember ("Application",

BindingFlags.getProperty, NULL, OCHART, NULL;

// Change the chart type to line.

Object [] Parameters = New Object [1];

Parameters [0] = 4; // xlline = 4

Ochart.gettype (). InvokeMember ("ChartType", BindingFlags.SetProperty,

Null, Ochart, Parameters;

// Update the chart image and quit msgraph.

Ochartapp.gettype (). InvokeMember ("Update",

Bindingflags.invokeMethod, NULL, OCHARTAPP, NULL;

Ochartapp.gettype (). InvokeMember ("quit",

Bindingflags.invokeMethod, NULL, OCHARTAPP, NULL;

// ... if desired, You can proceed from here Using The Microsoft Graph

// Object Model on the Ochart and Ochartapp Objects to make additional

// Changes to the chart.

// set the width of the chart.

Oshape.width = OWord.inchestopoints (6.25F);

Oshape.height = Oword.inchestopoints (3.57F);

// Add text after the chart.

Wrdrng = odoc.bookmarks.get_item (Ref OndofDoc) .range;

Wrdrng.insertparagraphafter ();

Wrdrng.insertafter ("the end.");

// Close this form.

THIS.CLOSE ();

}

Scroll to the top of the code window. Add the following code line to the end of the Using command list: use word = microsoft.Office.Interop.word;

Using system.reflection;

Press F5 to build and run the program. Click Button1, start the Word Automation function and create a document. After the code is executed, check the documentation you created for you. This document contains two pages of paragraphs, tables, and charts.

Back to top

Using templates If you want to use a document created with an automation, you will be easier to start the creation process using a new document of a template based on preset format. There are two advantages to the use of a template with the Word automation client compared to the head creation document:

You can add more control over the format settings and layout of the objects in the entire document. You can create a document using less code. By using templates, you can accurately adjust the layout of the table, paragraphs, and other objects in the document and add format settings to these objects. By using the automation function, create a new document based on the template containing the following code: Object Otemplate = "C: //MyTemplate.dot";

Odoc = OWord.Documents.add (Ref Otemplate, Ref Omissing,

Ref omissing, ref omissing;

In the template, you can define bookmarks, so that the automated client can add variable text at specific locations of the document, as shown below:

Object Obookmark = "MyBookmark";

Odoc.bookmarks.Item (Ref Obookmark) .Range.Text = "Some Text Here";

Another advantage of using templates is that you can create and store format styles that wish to apply at runtime, as shown below:

Object ostylename = "mystyle";

odoc.bookmarks.item (ref comookmark) .Range.set_style (Ref OstyleName);

- or -

Object ostylename = "mystyle";

OWord.Selection.Set_Style (Ref OstyleName);

Back to top

Refer to additional information, click the article number below to see the article in the Microsoft Knowledge Base:

301659 HOWTO: Use Visual C # .NET to make Microsoft Word automatically perform mail merge

302902 HOWTO: Use Visual C # .NET to make Office Automation Server Bindings For more information, visit the following Microsoft Developer Network (MSDN) website:

Microsoft Office Development with Visual Studio (Microsoft Office with Visual Studio development) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoffdev/html/vsofficedev.aspWord in the Office (MSDN online article) (Word in Office) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office112000.aspone More Word (MSDN Online Article) (Supplementary Description (MSDN online article) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office12072000.asp

Back to top

The information in this article applies to:

Microsoft Visual C # .NET (2002) Microsoft Visual C # .NET (2003) Microsoft Word 2002

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

New Post(0)