我这里倒碰巧有些例子,一些基本的操作都有了,不知道是不是你想要的,你自己看看吧.
unit WordDoc;
interface
uses
SysUtils, ComObj, Dialogs, ExtCtrls, WordXP;
type
TOleWordDoc = class(TObject)
private
FWordDoc: OleVariant;
FWordApp: OleVariant;
procedure SetVisible(const Value: Boolean);
public
constructor Create;
destructor Destroy; override;
procedure Close;
//添加文档
function AddDoc(FileName: String): Boolean;
//保存
procedure SaveAs(FileName: String);
//文尾插入
procedure InsertAfter(Text: String; Style: OleVariant);
//指定段插入
procedure Insert(ParagNum: Integer; Text: String; Style: OleVariant);
//插入页眉页脚
procedure InsertHeaderFooter(ImageFileName: String; HeaderText, FooterText: String);
//插入图片
Procedure InsertImage(ParagNum: Integer; ImageFileName: String);
//创建空白文档
function NewABlankDoc: Boolean;
//创建标题
procedure CreateCaption(Caption: String; CaptionStyle: OleVariant);
//插入目录
procedure InsertTableOfContents(Start, End_: Integer);
property Visible:Boolean Write SetVisible;
end;
implementation
{ TOleWordDoc }
function TOleWordDoc.AddDoc(FileName: String): Boolean;
begin
FWordApp.Documents.Add(FileName);
FWordDoc := FWordApp.Documents.Item(1);
Result := True;
end;
procedure TOleWordDoc.Close;
begin
FWordDoc.Close;
end;
constructor TOleWordDoc.Create;
begin
FWordApp := CreateOleObject('Word.Application');
end;
procedure TOleWordDoc.CreateCaption(Caption: String;
CaptionStyle: OleVariant);
begin
FWordDoc.Range(0, 0).InsertParagraphBefore;
FWordDoc.Range(0, 0).InsertBefore(Caption);
FWordDoc.Paragraphs.Item(1).Style := CaptionStyle;
end;
destructor TOleWordDoc.Destroy;
var
SaveChanges, Format: OleVariant;
begin
// FWordApp.Quit(SaveChanges, Format);
inherited;
end;
procedure TOleWordDoc.Insert(ParagNum: Integer; Text: String;
Style: OleVariant);
begin
FWordDoc.Paragraphs.Item(ParagNum).Range.Text := Text+#13;
//格式
FWordDoc.Paragraphs.Item(ParagNum).Style := Style;
//缩进20个像素
FWordDoc.Paragraphs.Item(ParagNum).LeftIndent := 20;
//设置粗体
FWordDoc.Paragraphs.Item(ParagNum).Range.Font.Bold := True;
//字体大小
FWordDoc.Paragraphs.Item(ParagNum).Range.Font.Size := 18;
//字体
FWordDoc.Paragraphs.Item(ParagNum).Range.Font.Name := '宋体';
end;
procedure TOleWordDoc.InsertAfter(Text: String; Style: OleVariant);
begin
FWordDoc.Content.InsertParagraphAfter;
//文尾添加文本
FWordDoc.Range.InsertAfter(Text);
//格式
FWordDoc.Paragraphs.Item(FWordDoc.Paragraphs.Count).Style := Style;
end;
procedure TOleWordDoc.InsertHeaderFooter(ImageFileName: String; HeaderText, FooterText: String);
begin
//向页眉添加文字
FWordDoc.Sections.Item(1).Headers.Item(1).Range.Text := HeaderText;
//向页眉添加图片
//FWordDoc.Sections.Item(1).Headers.Item(2).Shapes.AddPicture(ImageFileName, True, True, 1, 1, 500, 30);
//向页眉添加页码
FWordDoc.Sections.Item(1).Headers.Item(1).PageNumbers.Add;
//像页脚添加文字
FWordDoc.Sections.Item(1).Footers.Item(1).Range.Text := FooterText;
//像页脚添加图片
//FWordDoc.Sections.Item(1).Footers.Item(2).Shapes.AddPicture(ImageFileName, True, True, 1, 1, 500, 30);
//向页眉添加页码
FWordDoc.Sections.Item(1).Footers.Item(1).PageNumbers.Add
end;
procedure TOleWordDoc.InsertImage(ParagNum: Integer; ImageFileName: String);
var
Shape: Variant;
begin
FWordDoc.InlineShapes.AddPicture(ImageFileName, False, True, FWordDoc.Range(2, 2));
//FWordDoc.InlineShapes.Item(1).left := 10;
// FWordDoc.InlineShapes.Item(1).HorizontalLineFormat.Alignment := $00000000;
end;
procedure TOleWordDoc.InsertTableOfContents(Start, End_: Integer);
var
Range, UseHeadingStyles, UpperHeadingLevel, LowerHeadingLevel,
UseFields, TableID, RightAlignPageNumbers, IncludePageNumbers,
AddedStyles, UseHyperlinks, HidePageNumbersInWeb, UseOutlineLevels: OleVariant;
begin
Range := FWordDoc.Range(Start, End_);
UseHeadingStyles := True;
UpperHeadingLevel := 1;
LowerHeadingLevel := 4;
UseFields := False;
RightAlignPageNumbers := True;
IncludePageNumbers := True;
UseHyperlinks := True;
HidePageNumbersInWeb := True;
UseOutlineLevels := False;
FWordDoc.TablesOfContents.Add(Range, UseHeadingStyles, UpperHeadingLevel, LowerHeadingLevel,
UseFields, TableID, RightAlignPageNumbers, IncludePageNumbers,
AddedStyles, UseHyperlinks, HidePageNumbersInWeb, UseOutlineLevels);
end;
function TOleWordDoc.NewABlankDoc: Boolean;
begin
FWordApp.Documents.Add;
FWordDoc := FWordApp.Documents.Item(1);
Result := True;
end;
procedure TOleWordDoc.SaveAs(FileName: String);
begin
FWordDoc.SaveAs(FileName);
end;
procedure TOleWordDoc.SetVisible(const Value: Boolean);
begin
FWordApp.Visible := True;
end;
end.