1、在当前程序目录下建立以标题字段命名的Word文件
exepath:=application.ExeName;
for index:=1 to length(exepath) do
if exepath[index]='\' then
i:=index;
exepath:=copy(exepath,1,i);
doc_file:=exepath+mc+'.doc';
以标题字段“mc”命名Word文件
try
Wordapplication1.connect;
except
messagedlg('没有安装Word',mterror,[mbok],0);
abort;
end;
Wordapplication1.Caption := 'XX计划书';
Wordapplication1.visible := true;
Worddocument1.activate;
2、设置纸张大小
Wordapplication1.ActiveDocument.PageSetup.PageWidth:=XXX;
Wordapplication1.ActiveDocument.PageSetup.PageHeight:=XXX;
Wordapplication1.ActiveDocument.PageSetup.LeftMargin := XX;
//设置左边距
Wordapplication1.ActiveDocument.PageSetup.rightMargin := XX;
//设置右边距
3、插入页码
var fpage,pagea:olevariant;
fpage:=true;
pagea:=wdAlignPageNumberCenter;
Wordapplication1.activedocument.sections.item(1).Footers.item(1).PageNumbers.Add(pagea,fpage);
4、设置页面横向打印
s:=Wordapplication1.selection.start;
e:=Wordapplication1.selection.start;
aa:=wdSectionBreakNextPage;
Wordapplication1.ActiveDocument.Range(s,e).InsertBreak(aa);
Wordapplication1.Selection.Start:=Wordapplication1.Selection.Start + 1;
s:=Wordapplication1.Selection.start;
e:=Wordapplication1.ActiveDocument.Content.End_;
Wordapplication1.ActiveDocument.Range(S,e).PageSetup.Orientation:=wdOrientLandscape;
5、设置字体、字号
Wordapplication1.Selection.Font.Size:=18;
Wordapplication1.Selection.Font.Name := '黑体';
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.TypeText(dbedit4.text);
//标题
Wordapplication1.Selection.Font.Size := 14;
Wordapplication1.Selection.Font.Name := '宋体';
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
Wordapplication1.Selection.TypeText(' '+trim(dbmemo1.text));
//正文
... ...
6、插入表格
Wordapplication1.Selection.Font.Size :=10;
adoquery2.Active:=false;
adoquery2.active:=true;
doc:=Wordapplication1.activedocument;
counts:=adoquery2.RecordCount;
//记录数决定表格的行数
t:=doc.tables.Add(Wordapplication1.selection.range,counts+1,5);//5列
t.cell(1,1).range.text:= '单位';
t.Cell(1,1).Width:=120;
t.cell(1,1).range.Paragraphs.Alignment:= wdAlignParagraphCenter;
t.cell(1,2).range.text:= '姓名';
... ...
//依次写入其他字段的表头
for i:=2 to counts+1 do
begin
t.cell(i,1).range.text:=adoquery2.field
byname('dw').asstring;
t.Cell(i,1).Width:=120;
t.cell(i,1).range.Paragraphs.Alignment:=
wdAlignParagraphCenter;
t.cell(i,2).range.text:=adoquery2.field
byname('xm').asstring;
... ...
Adoquery2.next;
End;