Generate WORD Documents under. Net (available for use in tests)

Source: Internet
Author: User
Tags 3x5 table

Environment: Window2003 + Net.2.0

Remember to introduce the Word dll file Interop. word. dll (if you cannot find it, contact me directly)
In the namespace, remember to add using System. Reflection;

// ============== Start to generate a Word document ====================== this section ======== ===
Private void button5_Click (object sender, EventArgs e)
{
// ============= The following program comes from the http://support.microsoft.com/kb/316384/zh-cn
Object oMissing = System. Reflection. Missing. Value;
Object oEndOfDoc = "\ endofdoc";/* \ endofdoc is a predefined bookmark */

// ============ Start to create a Word document ==================
Word. _ Application oWord = new Word. Application ();
Word. _ Document oDoc = oWord. Documents. Add (ref oMissing, ref oMissing );
OWord. Visible = true;

// Insert a piece of information at the beginning of the document
Word. Paragraph oPara1;
OPara1 = oDoc. Content. Paragraphs. Add (ref oMissing );
OPara1.Range. Text = "Description of tables in the TT_Article Database ";
OPara1.Range. Font. Bold = 1;
OPara1.Format. SpaceAfter = 24; // 24 pt spacing after paragraph.
OPara1.Range. InsertParagraphAfter ();

// Insert a paragraph at the end of the document.
Word. Paragraph oPara2;
Object oRng = oDoc. Bookmarks. get_Item (ref oEndOfDoc). Range;
OPara2 = 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 oEndOfDoc). Range;
OPara3 = oDoc. Content. Paragraphs. Add (ref oRng );
OPara3.Range. Text = "This is a sentence of normal text. Now here is a table :";
OPara3.Range. Font. Bold = 0;
OPara3.Format. SpaceAfter = 24;
OPara3.Range. InsertParagraphAfter ();

// Insert a 3x5 table, fill it with data, and make the first row
// Bold and italic.
Word. Table oTable;
Word. Range wrdRng = oDoc. Bookmarks. get_Item (ref oEndOfDoc). 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. Italic = 1;

// Add some text after the table.
Word. Paragraph oPara4;
ORng = oDoc. Bookmarks. get_Item (ref oEndOfDoc). Range;
OPara4 = oDoc. Content. Paragraphs. Add (ref oRng );
OPara4.Range. insertparagrapappsfore ();
OPara4.Range. Text = "And here's another table :";
OPara4.Format. SpaceAfter = 24;
OPara4.Range. InsertParagraphAfter ();

// Insert a 5x2 table, fill it with data, and change the column widths.
WrdRng = oDoc. Bookmarks. get_Item (ref oEndOfDoc). 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 columns 1 & 2
OTable. Columns [2]. Width = oWord. InchesToPoints (3 );

// Keep inserting text. When you get to 7 inches from top of
// Document, insert a hard page break.
Object oPos;
Double dPos = oWord. InchesToPoints (7 );
ODoc. Bookmarks. get_Item (ref oEndOfDoc). Range. InsertParagraphAfter ();
Do
{
WrdRng = oDoc. Bookmarks. get_Item (ref oEndOfDoc). Range;
WrdRng. ParagraphFormat. SpaceAfter = 6;
WrdRng. InsertAfter ("A line of text ");
WrdRng. InsertParagraphAfter ();
OPos = wrdRng. get_Information
(Word. WdInformation. wdVerticalPositionRelativeToPage );
}
While (dPos> = Convert. ToDouble (oPos ));
Object oCollapseEnd = Word. WdCollapseDirection. wdCollapseEnd;
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 oEndOfDoc). Range;
OShape = wrdRng. InlineShapes. AddOLEObject (ref oClassType, ref oMissing,
Ref oMissing,
Ref oMissing, ref oMissing, ref oMissing );

// Demonstrate use of late bound oChart and oChartApp objects
// 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 oEndOfDoc). Range;
WrdRng. InsertParagraphAfter ();
WrdRng. InsertAfter ("the end .");

// Close this form.
This. Close ();

}

If you are interested, go to Microsoft to download the Word VBA help document.

Welcome to the Microsoft Office Word 2003 VBA Language Reference

Welcome to the Microsoft Office Word 2003 Visual Basic for Applications (VBA) Language Reference. this reference contains conceptual overviews, programming tasks, samples, and references to guide you in developing solutions based on the Microsoft Office Word 2003.

Publish date of this reference: July, 2004 (version 2003)

This documentation contains the following sections:

  • What's NewProvides a list of new members by object and in alphabetical order.
  • ConceptsProvides important concepts for developing custom Office solutions.
  • ReferenceProvides reference materials for the Office object model.

2003 Microsoft Corporation. All rights reserved.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.