delphi中通过CreateOleObject操控Word


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

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

开发环境:delphi7+xp+word2003

 

 

 

 
uses comobj, word2000;
procedure TForm1.Button1Click(Sender: TObject);
var
  WordApp, WordDoc, WordTable, wordShape, tmpValue,table: OleVariant;
  fileName          : string;
begin
  WordApp := CreateOleObject('Word.Application');
  WordDoc := WordApp.Documents.Add;

  try
    WordDoc.PageSetup.LeftMargin := 0.39*72; // 1 英寸 = 72 磅
    WordDoc.PageSetup.RightMargin := 0.39*72; // 1 英寸 = 72 磅
    WordDoc.PageSetup.TopMargin := 1*72; // 1 英寸 = 72 磅
    WordDoc.PageSetup.BottomMargin  := 1*72; // 1 英寸 = 72 磅
    WordDoc.PageSetup.PaperSize := wdPaperA4; //A4纸

    WordApp.Selection.Font.Name := '黑体';
    WordApp.Selection.Font.Size := 22;//二号字体 单位:磅
    WordApp.Selection.Font.Bold := True;//字体加粗
    WordApp.Selection.Font.Color := wdColorBlue;//字体颜色
    WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter; //段落中文本居中
    WordApp.Selection.ParagraphFormat.LineSpacingRule := wdLineSpaceSingle;//单倍行距
    WordApp.Selection.TypeText('学  生  评  教  结  果');
    WordApp.Selection.TypeParagraph;//回车

    WordApp.Selection.Font.Size := 15;//小三字体 单位:磅
    WordApp.Selection.TypeText(format('教师:%s  科目:%s     年段:%s  班级:%s',['清幽傲竹','地理','高一','高一(1)班']));
    WordApp.Selection.TypeParagraph;//回车


    table := WordApp.ActiveDocument.Tables.Add(WordApp.ActiveDocument.Paragraphs.item(3).Range,3,6); //往第三段增加一表格(三行6列)


    table.range.text:=   '题目';
    table.range.Font.Name := '宋体';//针对整个表格
    //table.cell(1,2).range.Font.Name := '宋体'; //针对某一单元格
    table.range.Font.Size := 10.5;//五号字体 单位:磅
    table.range.Font.Bold := false;//字体不加粗
    table.range.Font.Color := wdColorBlack;//字体颜色
    table.Rows.Alignment := wdAlignRowCenter;//表格居中
    table.Borders.Item(wdBorderLeft).LineStyle:=wdLineStyleSingle;
    table.Borders.Item(wdBorderRight).LineStyle:=wdLineStyleSingle;
    table.Borders.Item(wdBorderTop).LineStyle:=wdLineStyleSingle;
    table.Borders.Item(wdBorderBottom).LineStyle:=wdLineStyleSingle;
    table.Borders.Item(wdBorderHorizontal).LineStyle:=wdLineStyleSingle;
    table.Borders.Item(wdBorderVertical).LineStyle:=wdLineStyleSingle;

    //第一行第一列
    table.cell(1,1).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(1,1).range.text:=   '序号';
    //table.Cell(1,1).Width := 0.39*72; //设置第一行第一列的宽度
    table.Columns.item(1).Width := 0.39*72; //设置第一列的宽度
    table.Cell(1,1).Height := 0.31*72; //设置第一列的高度

    //第一行第二列
    table.cell(1,2).VerticalAlignment := wdCellAlignVerticalCenter; //文本竖直居中
    table.cell(1,2).range.text:=   '题目';
    table.Columns.item(2).Width := 3.43*72;

    //第一行第三列
    table.cell(1,3).VerticalAlignment := wdCellAlignVerticalTop;
    WordApp.Selection.MoveRight(wdCell,2); //向右移2个单元格
    WordApp.Selection.TypeText('A');
    WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend); //wdExtend:所选内容向右扩展
    //WordApp.Selection.Font.Name := '宋体';
    WordApp.Selection.Font.Size := 12;
    WordApp.Selection.Font.Bold := True;
    WordApp.Selection.MoveRight(wdCharacter,1);
    WordApp.Selection.TypeParagraph;//回车
    WordApp.Selection.Font.Size := 9;
    WordApp.Selection.Font.Bold := false;
    WordApp.Selection.TypeText('做得非常好');
    table.Columns.item(3).Width := 0.75*72;

    //第一行第四列
    table.cell(1,4).VerticalAlignment := wdCellAlignVerticalTop;
    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格
    WordApp.Selection.TypeText('B');
    WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend);
    WordApp.Selection.Font.Size := 12;
    WordApp.Selection.Font.Bold := True;
    WordApp.Selection.MoveRight(wdCharacter,1);
    WordApp.Selection.TypeParagraph;//回车
    WordApp.Selection.Font.Size := 9;
    WordApp.Selection.Font.Bold := false;
    WordApp.Selection.TypeText('做得较好');
    table.Columns.item(4).Width := 0.64*72;

    //第一行第五列
    table.cell(1,5).VerticalAlignment := wdCellAlignVerticalTop;
    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格
    WordApp.Selection.TypeText('C');
    WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend);
    WordApp.Selection.Font.Size := 12;
    WordApp.Selection.Font.Bold := True;
    WordApp.Selection.MoveRight(wdCharacter,1);
    WordApp.Selection.TypeParagraph;//回车
    WordApp.Selection.Font.Size := 9;
    WordApp.Selection.Font.Bold := false;
    WordApp.Selection.TypeText('做得一般');
    table.Columns.item(5).Width := 0.64*72;
    
    //第一行第六列
    table.cell(1,6).VerticalAlignment := wdCellAlignVerticalTop;
    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格
    WordApp.Selection.TypeText('D');
    WordApp.Selection.MoveLeft(wdCharacter,1,wdExtend);
    WordApp.Selection.Font.Size := 12;
    WordApp.Selection.Font.Bold := True;
    WordApp.Selection.MoveRight(wdCharacter,1);
    WordApp.Selection.TypeParagraph;//回车
    WordApp.Selection.Font.Size := 9;
    WordApp.Selection.Font.Bold := false;
    WordApp.Selection.TypeText('有待改进');
    table.Columns.item(6).Width := 0.64*72;

    //第二行
    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第1列
    table.cell(2,1).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(2,1).range.Font.Size := 12;
    table.cell(2,1).range.Font.bold := true;
    table.cell(2,1).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(2,1).range.text := '1';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第2列
    table.cell(2,2).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(2,2).range.Font.Size := 10.5;
    table.cell(2,2).range.Font.bold := false;
    table.cell(2,2).VerticalAlignment := wdCellAlignVerticalTop;
    WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
    table.cell(2,2).range.text := '老师敬业爱岗、关心学生、言行文明、教书育人、为人师表。';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第3列
    table.cell(2,3).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(2,3).range.Font.Size := 12;
    table.cell(2,3).range.Font.bold := true;
    table.cell(2,3).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(2,3).range.text := '100';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第4列
    table.cell(2,4).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(2,4).range.Font.Size := 12;
    table.cell(2,4).range.Font.bold := true;
    table.cell(2,4).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(2,4).range.text := '0';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第5列
    table.cell(2,5).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(2,5).range.Font.Size := 12;
    table.cell(2,5).range.Font.bold := true;
    table.cell(2,5).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(2,5).range.text := '0';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第2行第6列
    table.cell(2,6).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(2,6).range.Font.Size := 12;
    table.cell(2,6).range.Font.bold := true;
    table.cell(2,6).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(2,6).range.text := '0';

    //第三行
    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第1列
    table.cell(3,1).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(3,1).range.Font.Size := 12;
    table.cell(3,1).range.Font.bold := true;
    table.cell(3,1).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(3,1).range.text := '2';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第2列
    table.cell(3,2).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(3,2).range.Font.Size := 10.5;
    table.cell(3,2).range.Font.bold := false;
    table.cell(3,2).VerticalAlignment := wdCellAlignVerticalTop;
    WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
    table.cell(3,2).range.text := '老师教学态度认真,对课程的讲解内容和方法作了精心地准备。';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第3列
    table.cell(3,3).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(3,3).range.Font.Size := 12;
    table.cell(3,3).range.Font.bold := true;
    table.cell(3,3).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(3,3).range.text := '70';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第4列
    table.cell(3,4).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(3,4).range.Font.Size := 12;
    table.cell(3,4).range.Font.bold := true;
    table.cell(3,4).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(3,4).range.text := '10';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第5列
    table.cell(3,5).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(3,5).range.Font.Size := 12;
    table.cell(3,5).range.Font.bold := true;
    table.cell(3,5).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(3,5).range.text := '10';

    WordApp.Selection.MoveRight(wdCell,1); //向右移1个单元格,以使焦点定位于 第3行第6列
    table.cell(3,6).range.Font.Name := '宋体'; //针对某一单元格
    table.cell(3,6).range.Font.Size := 12;
    table.cell(3,6).range.Font.bold := true;
    table.cell(3,6).VerticalAlignment := wdCellAlignVerticalCenter;
    table.cell(3,6).range.text := '10';

    //增加一行
    WordApp.Selection.MoveRight(wdCell,1);
    WordApp.Selection.TypeText('3');    


    fileName := ExtractFilePath(ParamStr(0)) + 'test.doc';
    WordDoc.saveas(fileName);
  finally
    WordDoc.Saved := True;
    WordDoc.Close;
    WordApp.Quit;
  end;
  ShowMessage('ok');
end;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值