delphi 7实现word文档的分页读取

问题仔细描述,如果仅仅把word中的内容读取出来,不保存非常的简单。
http://zhidao.baidu.com/question/217275843.html?fr=qrl&cid=93&index=4&fr2=query

 

uses ComObj,WordXp;

var wordapp, WordDoc, PageRange: Variant;
sContext: string;
i, nPageCounts, nStart, nEnd : Integer;
begin
wordapp := CreateOleObject('Word.Application');
try
wordapp.Visible := True;
if dlgOpen1.Execute = False then Exit;
WordDoc := wordapp.Documents.Open(dlgOPen1.FileName);
//文档总页数
nPageCounts := wordapp.Selection.Information[wdNumberOfPagesInDocument];

//如果只有一页 那么全选就OK了
if nPageCounts = 1 then
begin
wordapp.Selection.WholeStory;
mmo1.Lines.Add('=============第'+IntToStr(nPageCounts)+'页内容:===================');
mmo1.Lines.Add(wordapp.Selection.Text);
Exit;
end;

nStart := -1;
nEnd := -1;
//循环获取文档页中的内容
for i := 1 to nPageCounts do
begin
//定位到第i页
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToNext, IntToStr(i));
//如果第i页是最后一页 那么直接将光标移动到最后 并输出内容
if i = nPageCounts then
begin
wordapp.Selection.EndKey(wdStory,wdExtend);
sContext := WordApp.Selection.Range.Text;
mmo1.Lines.Add('=============第'+IntToStr(i)+'页内容:===================');
mmo1.Lines.Add(sContext);
Exit;
end;

//取第i页的页首位置作为开始位置
nStart := wordapp.Selection.Start;
//定位到i+1页
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToNext, IntToStr(i+1));
//取第i+1页的页首位置作为结束位置
nEnd := wordapp.Selection.Start;
//根据开始位置和结束位置确定文档选中的内容(第i页的内容)
WordDoc.Range(nStart,nEnd).Select;
sContext := WordDoc.Range.Text;
//输出内容
mmo1.Lines.Add('=============第'+IntToStr(i)+'页内容:===================');
mmo1.Lines.Add(sContext);
nStart := -1;
nEnd := -1;
end;
finally
wordapp.Quit;
end;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值