http://blog.csdn.net/csm2432/article/details/7692443

上图为最终的WORD内容,看似简单,实则不易(研究了一天才得出所要的结果)。

在开发的过程中,请配合Word中的宏与VBA帮助文档(Microsoft Office\OFFICE11\2052\VBAWD10.CHM)

开发环境:delphi7+xp+word2003

 

 

 

[delphi] view plain copy
 
  1.    
[delphi] view plain copy
 
  1. uses comobj, word2000;  
  2. procedure TForm1.Button1Click(Sender: TObject);  
  3. var  
  4.   WordApp, WordDoc, WordTable, wordShape, tmpValue,table: OleVariant;  
  5.   fileName          : string;  
  6. begin  
  7.   WordApp := CreateOleObject('Word.Application');  
  8.   WordDoc := WordApp.Documents.Add;  
  9.   
  10.   try  
  11.     WordDoc.PageSetup.LeftMargin := 0.39*72; // 1 英寸 = 72 磅  
  12.     WordDoc.PageSetup.RightMargin := 0.39*72; // 1 英寸 = 72 磅  
  13.     WordDoc.PageSetup.TopMargin := 1*72; // 1 英寸 = 72 磅  
  14.     WordDoc.PageSetup.BottomMargin  := 1*72; // 1 英寸 = 72 磅  
  15.     WordDoc.PageSetup.PaperSize := wdPaperA4; //A4纸  
  16.   
  17.     WordApp.Selection.Font.Name := '黑体';  
  18.     WordApp.Selection.Font.Size := 22;//二号字体 单位:磅  
  19.     WordApp.Selection.Font.Bold := True;//字体加粗  
  20.     WordApp.Selection.Font.Color := wdColorBlue;//字体颜色  
  21.     WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter; //段落中文本居中  
  22.     WordApp.Selection.ParagraphFormat.LineSpacingRule := wdLineSpaceSingle;//单倍行距  
  23.     WordApp.Selection.TypeText('学  生  评  教  结  果');  
  24.     WordApp.Selection.TypeParagraph;//回车  
  25.   
  26.     WordApp.Selection.Font.Size := 15;//小三字体 单位:磅  
  27.     WordApp.Selection.TypeText(format('教师:%s  科目:%s     年段:%s  班级:%s',['清幽傲竹','地理','高一','高一(1)班']));  
  28.     WordApp.Selection.TypeParagraph;//回车  
  29.   
  30.   
  31.     table := WordApp.ActiveDocument.Tables.Add(WordApp.ActiveDocument.Paragraphs.item(3).Range,3,6); //往第三段增加一表格(三行6列)  
  32.   
  33.   
  34.     table.range.text:=   '题目';  
  35.     table.range.Font.Name := '宋体';//针对整个表格  
  36.     //table.cell(1,2).range.Font.Name := '宋体'; //针对某一单元格  
  37.     table.range.Font.Size := 10.5;//五号字体 单位:磅  
  38.     table.range.Font.Bold := false;//字体不加粗  
  39.     table.range.Font.Color := wdColorBlack;//字体颜色  
  40.     table.Rows.Alignment := wdAlignRowCenter;//表格居中  
  41.     table.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;  
  42.     table.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;  
  43.     table.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;  
  44.     table.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;  
  45.     table.Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleSingle;  
  46.     table.Borders.Item(wdBorderVertical).LineStyle:=wdLineStyleSingle;  
  47.   
  48.     //第一行第一列  
  49.     table.cell(1,1).VerticalAlignment := wdCellAlignVerticalCenter;  
  50.     table.cell(1,1).range.text:=   '序号';  
  51.     //table.Cell(1,1).Width := 0.39*72; //设置第一行第一列的宽度  
  52.     table.Columns.item(1).Width := 0.39*72; //设置第一列的宽度  
  53.     table.Cell(1,1).Height := 0.31*72; //设置第一列的高度  
  54.   
  55.     //第一行第二列  
  56.     table.cell(1,2).VerticalAlignment := wdCellAlignVerticalCenter; //文本竖直居中  
  57.     table.cell(1,2).range.text:=   '题目';  
  58.     table.Columns.item(2).Width := 3.43*72;  
  59.   
  60.     //第一行第三列  
  61.     table.cell(1,3).VerticalAlignment := wdCellAlignVerticalTop;  
  62.     WordApp.Selection.MoveRight(wdCell,2); //向右移2个单元格  
  63.     WordApp.Selection.TypeText('A');  
  64.     WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend); //wdExtend:所选内容向右扩展  
  65.     //WordApp.Selection.Font.Name := '宋体';  
  66.     WordApp.Selection.Font.Size := 12;  
  67.     WordApp.Selection.Font.Bold := True;  
  68.     WordApp.Selection.MoveRight(wdCharacter,1);  
  69.     WordApp.Selection.TypeParagraph;//回车  
  70.     WordApp.Selection.Font.Size := 9;  
  71.     WordApp.Selection.Font.Bold := false;  
  72.     WordApp.Selection.TypeText('做得非常好');  
  73.     table.Columns.item(3).Width := 0.75*72;  
  74.   
  75.     //第一行第四列  
  76.     table.cell(1,4).VerticalAlignment := wdCellAlignVerticalTop;  
  77.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格  
  78.     WordApp.Selection.TypeText('B');  
  79.     WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend);  
  80.     WordApp.Selection.Font.Size := 12;  
  81.     WordApp.Selection.Font.Bold := True;  
  82.     WordApp.Selection.MoveRight(wdCharacter,1);  
  83.     WordApp.Selection.TypeParagraph;//回车  
  84.     WordApp.Selection.Font.Size := 9;  
  85.     WordApp.Selection.Font.Bold := false;  
  86.     WordApp.Selection.TypeText('做得较好');  
  87.     table.Columns.item(4).Width := 0.64*72;  
  88.   
  89.     //第一行第五列  
  90.     table.cell(1,5).VerticalAlignment := wdCellAlignVerticalTop;  
  91.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格  
  92.     WordApp.Selection.TypeText('C');  
  93.     WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend);  
  94.     WordApp.Selection.Font.Size := 12;  
  95.     WordApp.Selection.Font.Bold := True;  
  96.     WordApp.Selection.MoveRight(wdCharacter,1);  
  97.     WordApp.Selection.TypeParagraph;//回车  
  98.     WordApp.Selection.Font.Size := 9;  
  99.     WordApp.Selection.Font.Bold := false;  
  100.     WordApp.Selection.TypeText('做得一般');  
  101.     table.Columns.item(5).Width := 0.64*72;  
  102.       
  103.     //第一行第六列  
  104.     table.cell(1,6).VerticalAlignment := wdCellAlignVerticalTop;  
  105.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格  
  106.     WordApp.Selection.TypeText('D');  
  107.     WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend);  
  108.     WordApp.Selection.Font.Size := 12;  
  109.     WordApp.Selection.Font.Bold := True;  
  110.     WordApp.Selection.MoveRight(wdCharacter,1);  
  111.     WordApp.Selection.TypeParagraph;//回车  
  112.     WordApp.Selection.Font.Size := 9;  
  113.     WordApp.Selection.Font.Bold := false;  
  114.     WordApp.Selection.TypeText('有待改进');  
  115.     table.Columns.item(6).Width := 0.64*72;  
  116.   
  117.     //第二行  
  118.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第1列  
  119.     table.cell(2,1).range.Font.Name := '宋体'; //针对某一单元格  
  120.     table.cell(2,1).range.Font.Size := 12;  
  121.     table.cell(2,1).range.Font.bold := true;  
  122.     table.cell(2,1).VerticalAlignment := wdCellAlignVerticalCenter;  
  123.     table.cell(2,1).range.text := '1';  
  124.   
  125.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第2列  
  126.     table.cell(2,2).range.Font.Name := '宋体'; //针对某一单元格  
  127.     table.cell(2,2).range.Font.Size := 10.5;  
  128.     table.cell(2,2).range.Font.bold := false;  
  129.     table.cell(2,2).VerticalAlignment := wdCellAlignVerticalTop;  
  130.     WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;  
  131.     table.cell(2,2).range.text := '老师敬业爱岗、关心学生、言行文明、教书育人、为人师表。';  
  132.   
  133.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第3列  
  134.     table.cell(2,3).range.Font.Name := '宋体'; //针对某一单元格  
  135.     table.cell(2,3).range.Font.Size := 12;  
  136.     table.cell(2,3).range.Font.bold := true;  
  137.     table.cell(2,3).VerticalAlignment := wdCellAlignVerticalCenter;  
  138.     table.cell(2,3).range.text := '100';  
  139.   
  140.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第4列  
  141.     table.cell(2,4).range.Font.Name := '宋体'; //针对某一单元格  
  142.     table.cell(2,4).range.Font.Size := 12;  
  143.     table.cell(2,4).range.Font.bold := true;  
  144.     table.cell(2,4).VerticalAlignment := wdCellAlignVerticalCenter;  
  145.     table.cell(2,4).range.text := '0';  
  146.   
  147.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第5列  
  148.     table.cell(2,5).range.Font.Name := '宋体'; //针对某一单元格  
  149.     table.cell(2,5).range.Font.Size := 12;  
  150.     table.cell(2,5).range.Font.bold := true;  
  151.     table.cell(2,5).VerticalAlignment := wdCellAlignVerticalCenter;  
  152.     table.cell(2,5).range.text := '0';  
  153.   
  154.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第6列  
  155.     table.cell(2,6).range.Font.Name := '宋体'; //针对某一单元格  
  156.     table.cell(2,6).range.Font.Size := 12;  
  157.     table.cell(2,6).range.Font.bold := true;  
  158.     table.cell(2,6).VerticalAlignment := wdCellAlignVerticalCenter;  
  159.     table.cell(2,6).range.text := '0';  
  160.   
  161.     //第三行  
  162.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第1列  
  163.     table.cell(3,1).range.Font.Name := '宋体'; //针对某一单元格  
  164.     table.cell(3,1).range.Font.Size := 12;  
  165.     table.cell(3,1).range.Font.bold := true;  
  166.     table.cell(3,1).VerticalAlignment := wdCellAlignVerticalCenter;  
  167.     table.cell(3,1).range.text := '2';  
  168.   
  169.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第2列  
  170.     table.cell(3,2).range.Font.Name := '宋体'; //针对某一单元格  
  171.     table.cell(3,2).range.Font.Size := 10.5;  
  172.     table.cell(3,2).range.Font.bold := false;  
  173.     table.cell(3,2).VerticalAlignment := wdCellAlignVerticalTop;  
  174.     WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;  
  175.     table.cell(3,2).range.text := '老师教学态度认真,对课程的讲解内容和方法作了精心地准备。';  
  176.   
  177.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第3列  
  178.     table.cell(3,3).range.Font.Name := '宋体'; //针对某一单元格  
  179.     table.cell(3,3).range.Font.Size := 12;  
  180.     table.cell(3,3).range.Font.bold := true;  
  181.     table.cell(3,3).VerticalAlignment := wdCellAlignVerticalCenter;  
  182.     table.cell(3,3).range.text := '70';  
  183.   
  184.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第4列  
  185.     table.cell(3,4).range.Font.Name := '宋体'; //针对某一单元格  
  186.     table.cell(3,4).range.Font.Size := 12;  
  187.     table.cell(3,4).range.Font.bold := true;  
  188.     table.cell(3,4).VerticalAlignment := wdCellAlignVerticalCenter;  
  189.     table.cell(3,4).range.text := '10';  
  190.   
  191.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第5列  
  192.     table.cell(3,5).range.Font.Name := '宋体'; //针对某一单元格  
  193.     table.cell(3,5).range.Font.Size := 12;  
  194.     table.cell(3,5).range.Font.bold := true;  
  195.     table.cell(3,5).VerticalAlignment := wdCellAlignVerticalCenter;  
  196.     table.cell(3,5).range.text := '10';  
  197.   
  198.     WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第6列  
  199.     table.cell(3,6).range.Font.Name := '宋体'; //针对某一单元格  
  200.     table.cell(3,6).range.Font.Size := 12;  
  201.     table.cell(3,6).range.Font.bold := true;  
  202.     table.cell(3,6).VerticalAlignment := wdCellAlignVerticalCenter;  
  203.     table.cell(3,6).range.text := '10';  
  204.   
  205.     //增加一行  
  206.     WordApp.Selection.MoveRight(wdCell,1);  
  207.     WordApp.Selection.TypeText('3');      
  208.   
  209.   
  210.     fileName := ExtractFilePath(ParamStr(0)) + 'test.doc';  
  211.     WordDoc.saveas(fileName);  
  212.   finally  
  213.     WordDoc.Saved := True;  
  214.     WordDoc.Close;  
  215.     WordApp.Quit;  
  216.   end;  
  217.   ShowMessage('ok');  
  218. end;