第1个回答 2014-08-25
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
ComObj,wordXP;
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
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;
numb : Integer;
begin
try
//启动word应用程序
FWord:=GetActiveOleObject('Word.Application');
except
FWord := CreateOleObject('Word.Application');
end;
FWord.Visible := true;
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);
//设置第一个单元的背景颜色
Tables.Cell(1,1).Shading.BackgroundPatternColor := -738131969 ;
Except
on e: Exception do
begin
FWord.Quit;
FWord := Unassigned;
exit;
end;
end;
{
//关闭word
FWord.Quit;
FWord := Unassigned;
}
end;
end.