给你一个我刚刚编写的例子,你自己研究:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure InsertLines(LineNum:Integer);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
wordApp,wordDoc:Variant;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ComObj;
Const wdAlignParagraphLeft=0;
Const wdAlignParagraphCenter=1;
Const wdAlignParagraphRight=2;
Const wdAlignParagraphJustify=3;
Const wdAdjustNone=0;
Const wdGray25=16;
Const wdGoTOLine=3;
Const wdGoToLast=-1;
Const wdSendToNewDocument=0;
{$R *.DFM}
//在文档中插入空行
procedure TForm1.InsertLines(LineNum:Integer);
var
iCount:Integer;
begin
for iCount:=1 to LineNum do
wordApp.Selection.TypeParagraph;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
StrAdd:String;
i:Integer;
wrdSelection:Variant;
begin
//创建一个word对象
wordApp:=CreateOleObject('Word.Application');
wordApp.Visible:=true;
//创建一个word文档
wordDoc:=wordApp.Documents.Add();
wordDoc.Select;
wrdSelection:=wordApp.selection;
//在文档中插入内容
StrAdd:='教务管理员职务说明书';
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
wrdSelection.font.bold:=true;
wrdSelection.font.size:=15;
wrdSelection.font.Underline:=1;
wrdSelection.TypeText(StrAdd);
wrdSelection.font.Underline:=0;
wrdSelection.font.bold:=false;
wrdSelection.font.size:=11;
InsertLines(1);
//在文档中插入一个4行4列的表格,并格式化
wordDoc.Tables.Add(wrdSelection.Range,4,4,2,0);
wordDoc.Tables.Item(1).Borders.Item(1).LineStyle:=7;
wordDoc.Tables.Item(1).Borders.Item(2).LineStyle:=7;
wordDoc.Tables.Item(1).Borders.Item(3).LineStyle:=7;
wordDoc.Tables.Item(1).Borders.Item(4).LineStyle:=7;
for i:=1 to 4 do
begin
wordDoc.Tables.Item(1).Cell(i,1).Range.Bold:=true;
wordDoc.Tables.Item(1).Cell(i,3).Range.Bold:=true;
wordDoc.Tables.Item(1).Rows.Item(i).Range.Paragraphs.Alignment:=wdAlignParagraphCenter;
end;
//在第一个表格中插入内容
wordDoc.Tables.Item(1).Cell(1,1).Range.text:='岗位名称';
worddoc.tables.item(1).cell(1,2).range.InlineShapes.AddPicture('E:/heartsong/0003.gif',False,True);
wordDoc.Tables.Item(1).Cell(2,1).Range.InsertAfter('岗位级别');
wordDoc.Tables.Item(1).Cell(3,1).Range.InsertAfter('隶属部门');
wordDoc.Tables.Item(1).Cell(4,1).Range.InsertAfter('直接上级');
wordDoc.Tables.Item(1).Cell(1,3).Range.InsertAfter('岗位编号');
wordDoc.Tables.Item(1).Cell(2,3).Range.InsertAfter('现任职者');
wordDoc.Tables.Item(1).Cell(3,3).Range.InsertAfter('分支机构');
wordDoc.Tables.Item(1).Cell(4,3).Range.InsertAfter('直接下级');
//插入一个5行一列的表格:工作概述
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(5).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;
wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('一、工作概述:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');
//插入一个5行一列的表格:主要工作职责
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(6).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;
wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('二、主要工作职责:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');
//插入一个5行一列的表格:工作关系:
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(7).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;
wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('三、工作关系:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');
//插入一个5行一列的表格:工作权限:
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(8).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;
wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('四、工作权限:');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'一些内容');
//插入一个5行一列的表格:任职资格与要求:
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
wordDoc.Tables.Add(wrdSelection.Range,1,1,2,0);
wordDoc.Tables.Item(1).Rows.Item(9).Range.Paragraphs.Alignment:=wdAlignParagraphLeft;
wrdSelection.Font.Bold:=true;
wrdSelection.Font.Size:=13;
wrdSelection.TypeText('五、任职资格与要求::');
wrdSelection.font.bold:=false;
wrdSelection.Font.Size:=11;
wrdSelection.TypeText(chr(10)+'学历:'+'一些内容');
wrdSelection.TypeText(chr(10)+'工作经历:'+'一些内容');
wrdSelection.TypeText(chr(10)+'要求具备的胜任特质:'+'一些内容');
//插入编制人等内容
wordApp.Selection.GoTo(wdGotoLine,wdGoToLast);
wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
//编制人编制日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('编制人:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('编制人'+' ');
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('编制日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('编制日期');
InsertLines(1);
//审核人审核日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('审核人:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('审核人'+' ');
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('审核日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('审核日期');
InsertLines(1);
//批准人批准日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('批准人:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('批准人'+' ');
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('批准日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('批准日期');
InsertLines(1);
//操作员操作日期
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('操作员:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('操作员'+' ');
wrdSelection.Font.Bold:=true;
wrdSelection.TypeText('操作日期:');
wrdSelection.Font.Bold:=false;
wrdSelection.TypeText('操作日期');
//保存文档
end;
end.