delphi控制word 标题 字符和位置

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls ,OleCtnrs,comobj,Word2000, OleServer;//

type
  TForm1 = class(TForm)
    edtTitle: TEdit;
    btn1: TButton;
    edtDate: TEdit;
    edtlength: TEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  s:string;
begin
  s:=DateTimeToStr(Now);
  edtDate.Text:= Copy(s,1,Length(s)-3);

  edtTitle.Text:='一二三四五六七八九十';

  edtlength.Text:= '40';
end;

procedure TForm1.btn1Click(Sender: TObject);
var 
  FWord :Variant;
  FDoc :Variant;
  strSQL :string;
  
  iLen:integer;//每行字节数
  s:string;
  iDateLen:Integer;
  iTitleLen:integer;
  ispace:integer;
  i:Integer;
begin
  //首先创造对象,如果出现异常就做出提示

  //FWord := CreateOleObject('Word.Application'); //Word.Application.11 //2003时
  FWord := CreateOleObject('Word.Document.12'); //Word.Application.11 //2003时
  //WOrd程序的执行是否可见,值为false时,程序在后台运行。
  FWord.Application.Visible := false;


//现在打开的Word中,创建一个新的页面,然后在其中键入需要的内容
  
  FDOC :=FWord.Application.Documents.Add;
  {对于Word的新页面}
  FWord.Application.Selection.Font.Name :='宋体';
//  FWord.Application.Selection.Font.Size :=10;
//  FWord.Application.Selection.Font.Bold :=0;
  {worddoc.PageSetup.LinesPage := 每页行数;
  worddoc.PageSetup.CharsLine := 每行字数;}


  FDOC.PageSetup.CharsLine:= edtlength.Text;
//  FWord.Application.Selection.paragraphs.Alignment := wdAlignParagraphLeft;
//  FWord.Application.Selection.TypeText(edtdate.text);
  FWord.Application.Selection.paragraphs.Alignment := wdAlignParagraphLeft;

  ilen:= StrToInt(edtlength.Text);  //40个汉字
  ilen:= 2*ilen;
 
  iDateLen:= Length(edtDate.Text); //16
  iTitleLen:= Length(edtTitle.Text); //20

  ispace:=(iLen div 2)-(iTitleLen div 2)- (iDateLen);

  s:= edtDate.Text;
  for i:=0 to ispace-1 do
  begin
    s:=s+' ';
  end;
  s:= s+edttitle.Text;


  FWord.Application.Selection.TypeText(s);



 // FWord.Application.Selection.TypeText(' 新夏旅游减肥 ');
//  FWord.Application.Selection.Typeparagraph;//换行
//  FWord.Application.Selection.Typeparagraph;//换行
// FWord.Application.Selection.paragraphs.Alignment := wdAlignParagraphLeft;
//  FWord.Application.Selection.Font.Size :=10;
//  FWord.Application.Selection.Font.Bold :=0;
//  FWord.Application.Selection.TypeText(' 现将我社组团部的');
//  FWord.Application.Selection.TypeText(' 新夏旅游减肥 ');
//
//  FWord.Application.Selection.Typeparagraph;//换行
//  FWord.Application.Selection.Typeparagraph;//换行

  //保存文档
  //FWord.Application.Caption := '组团计划保存在: '+ExtractFilePath(application.ExeName)+'Reports\xx.doc';
  FDOC.SaveAS(ExtractFilePath(Application.ExeName)+'xx.doc');  //E:\myDelphi7\delphi控制word中时间和标题位置

  FWord.Application.Quit;
  FWord := Unassigned; 

end;

end.
posted @ 2020-01-09 08:42  创新创造学习整合套路  阅读(412)  评论(0编辑  收藏  举报