C# vba 操作 Word

时间:2024-01-05 08:39:20
  • 添加引用

Microsoft Word  *.0 Object Library

Microsoft Graph *.0 Object Library

  • 变量说明

Object oMissing = System.Reflection.Missing.Value;

object oEndOfDoc = "\\endofdoc";

  • 操作类说明

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);

  • 操作说明

1、添加页眉

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 = "页眉内容";

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;//设置右对齐

WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐

WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

2、设置文档的行间距

WordApp.Selection.ParagraphFormat.LineSpacing = 15f;

3、插入表格

//移动焦点并换行

object count = 14;

object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;

WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点

WordApp.Selection.TypeParagraph();//插入段落

//文档中创建表格

Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);

//设置表格样式

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;

//填充表格内容

newTable.Cell(1, 1).Range.Text = "产品详细信息表";

newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体

//合并单元格

newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));

WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中

WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

//填充表格内容

newTable.Cell(2, 1).Range.Text = "产品基本信息";

newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色

//合并单元格

newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));

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

//填充表格内容

newTable.Cell(3, 1).Range.Text = "品牌名称:";

newTable.Cell(3, 2).Range.Text = "BrandName";

//纵向合并单元格

newTable.Cell(3, 3).Select();//选中一行

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();

//在表格中增加行

WordDoc.Content.Tables[1].Rows.Add(ref oMissing);

4、落款

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”

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

5、插入文字并设置格式

//在文档开始插入段落.

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;    //24 pt spacing after paragraph.

oPara1.Range.InsertParagraphAfter();

//在文档尾部插入段落

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、插入图表(OLEObject方式)

#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 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);

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] = "第一季度";

dataSheet.Cells[1, 3] = "第二季度";

dataSheet.Cells[1, 4] = "第三季度";

dataSheet.Cells[1, 5] = "第四季度";

dataSheet.Cells[2, 1] = "东部";

dataSheet.Cells[2, 2] = "50";

dataSheet.Cells[2, 3] = "40";

dataSheet.Cells[2, 4] = "50";

dataSheet.Cells[2, 5] = "50";

dataSheet.Cells[3, 1] = "西部";

dataSheet.Cells[3, 2] = "60";

dataSheet.Cells[3, 3] = "70";

dataSheet.Cells[3, 4] = "80";

dataSheet.Cells[3, 5] = "60";

dataSheet.Cells[4, 1] = "中部";

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、插入图表(Chart对象方式)

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 = "2007 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);