delphi 操作 word

 

 

 
 
uses  ComObj,word2000
 
procedure TForm1.ExportWord();
var
FWord :Variant;
FDoc :Variant;
i,Row:Integer;
nodeString:Tstringlist;
nodeList:TList;
Tables,TablesSub:Variant;
sTableName,sUserName:string;
CfCversions,oReadOnly,AddToRctFiles,
PswDocument,PswTemplate,oRevert,WPswDocument,WPswTemplate,oFormat,Where,vActWin:Variant;
UserNameList:TStringList;
begin
  nodeList:=TList.Create;
  UserNameList:=TStringList.Create;
  try
    try
      //启动word应用程序
      FWord:=GetActiveOleObject('Word.Application');
    except
      FWord := CreateOleObject('Word.Application');
    end;
     FWord.Visible := False;
 
    try
    //创建一个word文档
    FDOC :=FWord.Documents.Add;
    vActWin   :=   FWord.ActiveWindow;
    FWord.Selection.Font.Name :='宋体'; //字体名称
    FWord.Selection.Font.Size :=20; //字体大小
    FWord.Selection.Font.Bold :=2; //字体加粗
 
    //设定文字对齐方式
    FWord.Selection.paragraphs.Alignment := wdAlignParagraphCenter;
    //添加文字内容
    FWord.Selection.TypeText('标题一');
    //插入回车符
    FWord.Selection.Typeparagraph;
 
    FWord.Selection.paragraphs.Alignment := wdAlignParagraphLeft;
    FWord.Selection.Font.Size :=12;
    FWord.Selection.Font.Bold :=0;
    FWord.Selection.Typeparagraph;
 
    //添加一个2行3列的表格
    Tables:=FWord.ActiveDocument.Tables.Add(Range:=FWord.Selection.Range,
    NumRows:=2,
    NumColumns:=3,
    DefaultTableBehavior:=1,
    AutoFitBehavior:=0);
    //设置第二行表格的高度
    Tables.Rows.Item(2).SetHeight(250,1);
 
    FWord.Selection.range.rows.headingformat:=wdtoggle;
 
    Tables.rows.allowbreakacrosspages:=wdtoggle;
 
 
    //设置表格第一行的对齐方式
     Tables.Rows.item(1).Alignment   :=   wdAlignRowCenter;
     //向表格添加内容
     Tables.Cell(1,1).Range.Text:='局长';
     Tables.Cell(1,2).Range.Text:='厅长';
     Tables.Cell(1,3).Range.Text:='部长';
     MoveDown(FWord,1);
     //插入一个正方形符号
     FWord.Selection.InsertSymbol(Font:='+中文正文', CharacterNumber:=9633, Unicode:=True);
     FWord.Selection.TypeText('建议以下述方式做'+#13      +'工作的总结'+#13);
    FWord.Selection.Typeparagraph;
 
    TablesSub:=FWord.ActiveDocument.Tables.Add(Range:=FWord.Selection.Range,
    NumRows:=1,
    NumColumns:=1,
    DefaultTableBehavior:=1,
    AutoFitBehavior:=0);
 
    TablesSub.Columns.Item(1).SetWidth(136,1);
    FWord.Selection.TypeText('    ');
    //插入一个园形符号
    FWord.Selection.InsertSymbol(Font:='+中文正文', CharacterNumber:=9675, Unicode:=True);
    FWord.Selection.TypeText('     ');
    FWord.Selection.InsertSymbol(Font:='+中文正文', CharacterNumber:=9675, Unicode:=True);
    FWord.Selection.TypeText('     ');
    FWord.Selection.InsertSymbol(Font:='+中文正文', CharacterNumber:=9675, Unicode:=True);
    FWord.Selection.TypeText(#13);
    FWord.Selection.TypeText(' '+'总结会议'+' '+'公函'+'  '+'电话');
    MoveDown(FWord,1);
    FWord.Selection.TypeText('意见');
    Tables.Cell(2,2).Range.Text:=''+#13+'意见';
    Tables.Cell(2,3).Range.Text:=''+#13+'批示';
    MoveEnd(FWord);
 
    for i:=1 to 30 do
    begin
    FWord.Selection.TypeText('');
    FWord.Selection.Typeparagraph;
    end;
 
    //设置边距  上边距topmargin 下bottommargin 左leftmargin 右 right.....
    FWord.ActiveDocument.pagesetup.topmargin:=20;
    FWord.ActiveDocument.pagesetup.bottommargin:=40;
    FWord.ActiveDocument.pagesetup.leftmargin:=80;
    FWord.ActiveDocument.pagesetup.rightmargin:=100;
 
 
    //操作页尾
     FWord.ActiveWindow.ActivePane.View.SeekView   :=   wdSeekCurrentPageFooter;
     FWord.Application.Selection.ParagraphFormat.Alignment   :=   wdAlignParagraphLeft;
     FWord.Application.Selection.TypeText('人员'+'UserNameList.CommaText');
     FWord.Selection.Typeparagraph;
     FWord.Application.Selection.TypeText('日期'+FormatDateTime('YYYY-MM-DD',now));
     FWord.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;
 
 
 
    Except
    on e: Exception do
    begin
      FWord.Quit;
      FWord := Unassigned;
      exit;
    end;
    end;
    FWord.Caption :=ExtractFilePath(application.ExeName)+'name.doc';
   //保存为97-2003格式
    FDOC.SaveAS(ExtractFilePath(application.ExeName)+'审计简报.doc',wdFormatDocument);
    //FDOC.SaveAS(ExtractFilePath(application.ExeName)+'name.doc');
    oReadOnly:=False;
    CfCversions := EmptyParam;
    AddToRctFiles:= EmptyParam;
    PswDocument:= EmptyParam;
    PswTemplate:= EmptyParam;
    oRevert:= EmptyParam;
    WPswDocument:= EmptyParam;
    WPswTemplate:= EmptyParam;
    oFormat:= EmptyParam;
    FWord.Documents.open(FWord.Caption,CfCversions,oReadOnly,AddToRctFiles,
    PswDocument,PswTemplate,oRevert,WPswDocument,WPswTemplate,oFormat);
  finally
     Screen.Cursor:=crdefault;
     nodeList.free;
     UserNameList.Free;
  end;
end;
procedure TForm1.MoveEnd(fword:olevariant);
var
unit_: olevariant;
extend: olevariant;
begin
  unit_ := wdstory;
  extend := wdmove;
  fword.selection.endkey(unit_, extend);
end;
 
procedure TForm1.movedown(fword:olevariant;num: integer = 1);
var
unit_: olevariant;
count: olevariant;
extend: olevariant;
begin
  try
  count := num;
  unit_ := wdline;
  fword.selection.movedown(unit_, count, extend);
  except
  end;
end;
procedure TForm1.moveup(fword:olevariant;num: integer = 1);
var
unit_: olevariant;
count: olevariant;
extend: olevariant;
begin
  unit_ := wdline;
  count := 1;
  fword.selection.moveup(unit_, count, extend);
end;
原文地址:https://www.cnblogs.com/zhangzhifeng/p/5249810.html