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;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章