楼主,俺来领分啦~~
测试通过,Demo程序可到http://crazycock.ys168.com 下载。
------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WordXP, OleServer;
type
TForm1 = class(TForm)
Button1: TButton;
WordDocument1: TWordDocument;
WordApplication1: TWordApplication;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
DocFile:OleVariant;
CurPara:OleVariant;
CurPageCout:Integer;
begin
WordApplication1.Connect;
WordApplication1.Visible:=True; //如果为False则看不见Word,但是别忘了用完就关掉
DocFile:=ExtractFilePath(ParamStr(0))+'记事本.doc';
//打开doc
WordDocument1.ConnectTo(WordApplication1.Documents.Open(DocFile,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam));
//关闭拼音查找和语法查找,以便提高程序运行的效率
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;
// ShowMessage('目前有'+IntToStr(WordDocument1.Paragraphs.Count)+'段。');
// ShowMessage('目前有'+IntToStr(WordDocument1.Sections.Count)+'节。');
// ShowMessage('目前有'+IntToStr(WordDocument1.Sentences.Count)+'句。');
// ShowMessage('目前有'+IntToStr(WordApplication1.Selection.Information[wdNumberOfPagesInDocument])+'页');
//先插入两行再说,这两行是你需要的两行置底的文字
WordDocument1.Paragraphs.Add(EmptyParam);
CurPara:=WordDocument1.Paragraphs.Count;
WordDocument1.Paragraphs.Item(CurPara).Range.Font.Color:=wdColorRed;
WordDocument1.Paragraphs.Item(CurPara).Range.Font.Size:=20;
WordDocument1.Paragraphs.Item(CurPara).Range.Text:='末尾一行!!!';
//
WordDocument1.Paragraphs.Add(EmptyParam);
CurPara:=WordDocument1.Paragraphs.Count;
WordDocument1.Paragraphs.Item(CurPara).Range.Font.Color:=wdColorGreen;
WordDocument1.Paragraphs.Item(CurPara).Range.Font.Size:=10;
WordDocument1.Paragraphs.Item(CurPara).Range.Text:='末尾二行!!!';
//开始添加空行,直到页数发生变化
CurPageCout:=WordApplication1.Selection.Information[wdNumberOfPagesInDocument];
repeat
CurPara:=WordDocument1.Paragraphs.Count-1;//加了末尾两行后有几段文字,减去1得到"末尾一行"的位置
CurPara:=WordDocument1.Paragraphs.Item(CurPara).Range;
WordDocument1.Paragraphs.Add(CurPara);
until WordApplication1.Selection.Information[wdNumberOfPagesInDocument]>CurPageCout;
WordDocument1.Undo;//上面的做法,其实已经跑多了一行,当然要Undo一下。
ShowMessage('搞定!');
//保存、退出
//.........
end;
end.