delphi 循环读取word每页的内容显示在OleContainer1 能否帮忙翻译成bcd
procedure TForm1.Button7Click(Sender: TObject);
var
wordapp, WordDoc, PageRange: Variant;
i, nPageCounts, nStart, nEnd : Integer;
sContext: string;
begin
//禁止双击打开word编辑
olecontainer1.AutoActivate := aaManual;
//禁止右键菜单
olecontainer1.AutoVerbMenu := False;
wordapp := CreateOleObject('Word.Application');
try
wordapp.Visible := False;
WordDoc := wordapp.Documents.Open(AppPath+'7S.doc');
//文档总页数
nPageCounts := wordapp.Selection.Information[wdNumberOfPagesInDocument];
//如果只有一页 那么全选就OK了
if nPageCounts = 1 then
begin
wordapp.Selection.WholeStory;
wordapp.selection.copy;
if olecontainer1.canpaste then
olecontainer1.paste;
OleContainer1.Update;
Exit;
end;
nStart := 0;
nEnd := 0;
//循环获取文档页中的内容
for i := 1 to nPageCounts do
begin
//定位到第i页
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToAbsolute, IntToStr(i));
//如果第i页是最后一页 那么直接将光标移动到最后 并输出内容
if i = nPageCounts then
begin
wordapp.Selection.EndKey(wdStory,wdExtend);
wordapp.selection.copy;
if OleContainer1.canpaste then
OleContainer1.paste;
OleContainer1.Update;
Exit;
end;
//取第i页的页首位置作为开始位置
//nStart := wordapp.Selection.Start;
nStart := nEnd;
//定位到i+1页
PageRange := wordapp.Selection.GoTo(wdGoToPage, wdGoToAbsolute, IntToStr(i+1));
//取第i+1页的页首位置作为结束位置
nEnd := wordapp.Selection.Start;
//showmessage(IntToStr(nStart)+';'+IntToStr(nEnd));
//根据开始位置和结束位置确定文档选中的内容(第i页的内容)
WordDoc.Range(nStart,nEnd).Select;
sContext := WordDoc.Range.Text;
//ShowMessage(sContext);
//wordapp.Selection.WholeStory;
wordapp.selection.copy;
if olecontainer1.canpaste then
olecontainer1.paste;
OleContainer1.Update;
Sleep(3000);
end;
finally
wordapp.Quit;
end;
end;