C # VBA Action Word

Source: Internet
Author: User

    • Add Reference

Microsoft Word *.0 Object Library

Microsoft Graph *.0 Object Library

    • Variable description

Object omissing = System.Reflection.Missing.Value;

Object oEndOfDoc = "\\endofdoc";

    • Operation class Description

Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass ();

Microsoft.Office.Interop.Word.Document WordDoc = WORDAPP.DOCUMENTS.ADD (ref omissing, ref omissing, ref omissing, ref omissing);

    • Operating instructions

1. Add a header

if (WordApp.ActiveWindow.ActivePane.View.Type = = Microsoft.Office.Interop.Word.WdViewType.wdNormalView | | WordApp.ActiveWindow.ActivePane.View.Type = = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView)

{

WordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;

}

WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;

String sheader = "header content";

WordApp.Selection.HeaderFooter.LinkToPrevious = false;

WordApp.Selection.HeaderFooter.Range.Text = Sheader;

WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;

WordApp.Selection.ParagraphFormat.Alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphright;//Set the right alignment

WordApp.Selection.ParagraphFormat.Alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphleft;//Set the left alignment

WordApp.ActiveWindow.View.SeekView = wdseekview.wdseekmaindocument;//jump out of header settings

2. Set the line spacing of the document

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;

3. Insert Table

Move focus and Wrap

Object count = 14;

Object wdline = microsoft.office.interop.word.wdunits.wdline;//is changed by one line;

WordApp.Selection.MoveDown (ref wdline, ref count, ref omissing);//Mobile Focus

WordApp.Selection.TypeParagraph ();//Insert Paragraph

Create a table in a document

Microsoft.Office.Interop.Word.Table newtable = WordDoc.Tables.Add (WordApp.Selection.Range, 3, ref omissing, ref omissing);

Set Table Style

NewTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;

NewTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

NEWTABLE.COLUMNS[1]. Width = 100f;

NEWTABLE.COLUMNS[2]. Width = 220f;

NEWTABLE.COLUMNS[3]. Width = 105f;

Populating table Contents

Newtable.cell (1, 1). Range.Text = "Product detail table";

Newtable.cell (1, 1). Range.bold = 2;//The font in the Set cell is bold

Merge cells

Newtable.cell (1, 1). Merge (Newtable.cell (1, 3));

WordApp.Selection.Cells.VerticalAlignment = microsoft.office.interop.word.wdcellverticalalignment.wdcellalignverticalcenter;//Vertical Center

WordApp.Selection.ParagraphFormat.Alignment = microsoft.office.interop.word.wdparagraphalignment.wdalignparagraphcenter;//Horizontal Center

Populating table Contents

Newtable.cell (2, 1). Range.Text = "Product basic information";

Newtable.cell (2, 1). Range.Font.Color = microsoft.office.interop.word.wdcolor.wdcolordarkblue;//Sets the font color in the cell

Merge cells

Newtable.cell (2, 1). Merge (Newtable.cell (2, 3));

WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

Populating table Contents

Newtable.cell (3, 1). Range.Text = "brand Name:";

Newtable.cell (3, 2). Range.Text = "brandname";

Merge cells vertically

Newtable.cell (3, 3). Select ();//Select a row

Object moveunit = Microsoft.Office.Interop.Word.WdUnits.wdLine;

Object movecount = 5;

Object moveextend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;

WordApp.Selection.MoveDown (ref moveunit, ref movecount, ref moveextend);

WordApp.Selection.Cells.Merge ();

Add rows to a table

WORDDOC.CONTENT.TABLES[1]. Rows.Add (ref omissing);

4. Inscription

WordDoc.Paragraphs.Last.Range.Text = "Document creation time:" + DateTime.Now.ToString ();//"inscription"

WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

5. Inserting text and formatting

Inserts a paragraph at the beginning of the document.

Microsoft.Office.Interop.Word.Paragraph OPara1;

OPara1 = WORDDOC.CONTENT.PARAGRAPHS.ADD (ref omissing);

OPara1.Range.Text = "Heading 1";

OPara1.Range.Font.Bold = 1;

OPara1.Format.SpaceAfter = 24; PT spacing after paragraph.

OPara1.Range.InsertParagraphAfter ();

Insert a paragraph at the end of a document

Microsoft.Office.Interop.Word.Paragraph OPara2;

Object orng = WordDoc.Bookmarks.get_Item (ref oEndOfDoc). Range;

OPara2 = WORDDOC.CONTENT.PARAGRAPHS.ADD (ref orng);

OPara2.Range.Text = "Heading 2";

OPara2.Format.SpaceAfter = 6;

OPara2.Range.InsertParagraphAfter ();

6. Insert Chart (OLEObject mode)

#region

Object oEndOfDoc = "\\endofdoc";

Microsoft.Office.Interop.Word.Range wrdrng;

Insert a chart.

Microsoft.Office.Interop.Word.InlineShape oshape;

Object oClassType = "Msgraph.chart";

wrdrng = WordDoc.Bookmarks.get_Item (ref oEndOfDoc). Range;

oshape = WrdRng.InlineShapes.AddOLEObject (ref oclasstype, ref omissing, ref omissing, ref omissing, ref omissing, ref OMis Sing, 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);

The change of the chart type to line.

object[] Parameters = new Object[1];

Parameters[0] = 4; Xlline = 4

Ochart.gettype (). InvokeMember ("ChartType", BindingFlags.SetProperty, NULL, ochart, Parameters);

Microsoft.Office.Interop.Graph.Chart objchart = (Microsoft.Office.Interop.Graph.Chart) oShape.OLEFormat.Object;

Objchart.charttype = Microsoft.Office.Interop.Graph.XlChartType.xlColumnClustered;

Microsoft.Office.Interop.Graph.DataSheet datasheet;

datasheet = ObjChart.Application.DataSheet;

Datasheet.cells[1, 2] = "First quarter";

Datasheet.cells[1, 3] = "second quarter";

Datasheet.cells[1, 4] = "third quarter";

Datasheet.cells[1, 5] = "Quarter four";

Datasheet.cells[2, 1] = "East";

Datasheet.cells[2, 2] = "50";

Datasheet.cells[2, 3] = "40";

Datasheet.cells[2, 4] = "50";

Datasheet.cells[2, 5] = "50";

Datasheet.cells[3, 1] = "West";

Datasheet.cells[3, 2] = "60";

Datasheet.cells[3, 3] = "70";

Datasheet.cells[3, 4] = "80";

Datasheet.cells[3, 5] = "60";

Datasheet.cells[4, 1] = "Middle";

Datasheet.cells[4, 2] = "50";

Datasheet.cells[4, 3] = "40";

Datasheet.cells[4, 4] = "50";

Datasheet.cells[4, 5] = "50";

ObjChart.Application.Update ();

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 = Wordapp.inchestopoints (6.25f);

Oshape.height = Wordapp.inchestopoints (3.57f);

Add text after the chart.

wrdrng = WordDoc.Bookmarks.get_Item (ref oEndOfDoc). Range;

Wrdrng.insertparagraphafter ();

wrdRng.InsertAfter ("the END.");

#endregion

7, insert chart (Chart object mode)

Microsoft.Office.Interop.Word.Chart Wdchart =worddoc.inlineshapes.addchart ( Microsoft.Office.Core.XlChartType.xlColumnClustered, ref omissing). Chart;

Microsoft.Office.Interop.Word.ChartData ChartData = Wdchart.chartdata;

Microsoft.Office.Interop.Excel.Workbook Dataworkbook = (Microsoft.Office.Interop.Excel.Workbook) Chartdata.workbook;

DataWorkbook.Application.Visible = false;

Microsoft.Office.Interop.Excel.Worksheet datasheet = (Microsoft.Office.Interop.Excel.Worksheet) DATAWORKBOOK.WORKSHEETS[1];

Microsoft.Office.Interop.Excel.Range Trange = DataSheet.Cells.get_Range ("A1", "B5");

Microsoft.Office.Interop.Excel.ListObject tbl1 = datasheet.listobjects[1];

Tbl1. Resize (Trange);

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("A2", omissing)). FORMULAR1C1 = "Bikes";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("A3", omissing)). FORMULAR1C1 = "Accessories";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("A4", omissing)). FORMULAR1C1 = "Repairs";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("A5", omissing)). FORMULAR1C1 = "Clothing";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("B2", omissing)). FORMULAR1C1 = "1000";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("B3", omissing)). FORMULAR1C1 = "2500";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("B4", omissing)). FORMULAR1C1 = "4000";

((Microsoft.Office.Interop.Excel.Range) dataSheet.Cells.get_Range ("B5", omissing)). FORMULAR1C1 = "3000";

WdChart.ChartTitle.Font.Italic = true;

WdChart.ChartTitle.Font.Size = 18;

WdChart.ChartTitle.Font.Color = Color.Black.ToArgb ();

WdChart.ChartTitle.Text = "Sales";

WdChart.ChartTitle.Format.Line.Visible =

Microsoft.Office.Core.MsoTriState.msoTrue;

WdChart.ChartTitle.Format.Line.ForeColor.RGB =color.black.toargb ();

Wdchart.applydatalabels (microsoft.office.interop.word.xldatalabelstype.xldatalabelsshowlabel,omissing, oMissing , omissing, omissing, omissing, omissing, omissing, omissing,

omissing);

C # VBA Action Word

Related Article

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.