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;
//删除空白段
function DeleteNullLines: 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;
function TOleWordDoc.DeleteNullLines: Integer;
var
I, ParaCount, DelCount: Integer;
Str: String;
begin
DelCount := 0;
ParaCount := FWordDoc.Paragraphs.Count;
for I := 1 to ParaCount do
begin
Str := Trim(FWordDoc.Paragraphs.Item(I).Range.Text);
if SameText(Str, '') then
begin
FWordDoc.Paragraphs.Item(I).Range.Delete;
DelCount := DelCount + 1;
end;
end;
Result := DelCount;
end;
destructor TOleWordDoc.Destroy;
var
SaveChanges, Format: OleVariant;
begin
// FWordApp.Quit(SaveChanges, Format);
inherited;
end;
procedure TOleWordDoc.SetVisible(const Value: Boolean);
begin
FWordApp.Visible := True;
end;
end.