用DELPHI如何删除WORD文档中的空行?(50分)

  • 主题发起人 主题发起人 czx_china
  • 开始时间 开始时间
C

czx_china

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,就是把只有回车键的这行删除了
 
一行一行的读,如果只是回车或者换行符号就删除可以不?
 
procedure TOleWordDoc.DeleteNullLines;
var
I, ParaCount: Integer;
Str: String;
begin
//段数
ParaCount := FWordDoc.Paragraphs.Count;
for I := 1 to ParaCount do
begin
//获取该段文本
Str := Trim(FWordDoc.Paragraphs.Item(I).Range.Text);
if SameText(Str, '') then
//如果该段文本为空,删除
FWordDoc.Paragraphs.Item(I).Range.Delete;
end;
end;
 
太好了.
 
根本编译不过去啊!
 
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.
 
调用WORD的替换接口
 
原来因为我是用TWordApplication,用这个怎么做呢?
 
那就不知道了,
看其他人知道吗?
 
后退
顶部