求Delphi控制word翻页显示的代码

yct0605 2011-12-27 09:35:58
有一个word文档带图片和文字信息(页数不超过10页),要用delphi显示word的内容(默认显示的是第一页内容),并控制word翻页显示,多谢大家了!
...全文
393 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mayyang610 2012-01-06
  • 打赏
  • 举报
回复
这样通过复制粘贴的方法显示word每一页的内容,有一个问题,如果整个word文档有项目编号的话,那么除了第一页的项目编号复制出来是正确的,后面的所有页中的项目编号复制出来再粘贴在olecontainer里时,编号都会发生变化,这是由于word的自动编号导致的。不知道楼主有没有解决办法?
erhan 2011-12-28
  • 打赏
  • 举报
回复
aa := wdGoToPage;
bb := wdGoToAbsolute;
cc := '4';
dd := '';
我那2段代码,都可以任意页尼,cc:='4'表示跳到第4页。我的调用跟ta的应该是一样的,ta不应该只能显示第1和最后1页的。从代码的逻辑上看,后面
// wordapp.Selection.WholeStory;
// wordapp.selection.copy;

{ if olecontainer1.canpaste then
olecontainer1.paste;

OleContainer1.Update; }

//Sleep(3000);

取消注释即可。
yct0605 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 erhan 的回复:]
引用 3 楼 yct0605 的回复:
这个是打开word来控制的,我想要集成在form里面来实现啊!


哦,网上查了一下,要集成的话可以用OleContainer;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form……
[/Quote]

这个默认还是跳转到第一页啊
yct0605 2011-12-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 erhan 的回复:]
aa := wdGoToPage;
bb := wdGoToAbsolute;
cc := '4';
dd := '';
我那2段代码,都可以任意页尼,cc:='4'表示跳到第4页。我的调用跟ta的应该是一样的,ta不应该只能显示第1和最后1页的。从代码的逻辑上看,后面
// wordapp.Selection.WholeStory;
// wordapp.selectio……
[/Quote]
你的代码我也测试了,是可以实现的,就是ole里面包含了菜单,这个菜单我不想要,而且ole打开的word文档只读就可以了,我想实现的是自动循环,从第一页循环到最后一页然后在重新开始循环,我在网上的代码测试了就是不能显示中间页的内容,注释的部分加上也不可以,真是奇怪了。
erhan 2011-12-28
  • 打赏
  • 举报
回复
不客气,借机也学点东东,呵呵。
yct0605 2011-12-28
  • 打赏
  • 举报
回复
已经可以实现了,多谢了。
erhan 2011-12-28
  • 打赏
  • 举报
回复
网上的代码有点问题,改了一下,已经实现你要的效果了。

procedure TForm1.Button1Click(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('d:\test.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);
{
nStart := -1;
nEnd := -1;
}
end;
finally
wordapp.Quit;
end;
end;
erhan 2011-12-27
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, Word2000, ComObj;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
word: Variant;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Enabled := True;
Button2.Enabled := False;
Button3.Enabled := False;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
try
word.Quit;
except
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
word := CreateOleObject( 'Word.Application' );
word.Documents.Open('d:\test.doc');
word.Visible := True;
Button1.Enabled := False;
Button2.Enabled := True;
Button3.Enabled := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
word.Selection.GoTo(wdGoToPage, wdGoToAbsolute, '1');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
word.Selection.GoTo(wdGoToPage, wdGoToAbsolute, '4');
end;

end.

//仅示例,如何延时控制,timer你懂的。
yct0605 2011-12-27
  • 打赏
  • 举报
回复
在补充一点吧:程序默认打开word的第一页,可以设置延时时间自动跳转到第二页,一次类推
yct0605 2011-12-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 erhan 的回复:]
引用 3 楼 yct0605 的回复:
这个是打开word来控制的,我想要集成在form里面来实现啊!


哦,网上查了一下,要集成的话可以用OleContainer;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form……
[/Quote]
多谢了,我在网上也找了一个,就是只显示第一页和最后一页,麻烦你帮我看看。
[

[code=Delphi(Pascal)]
var
wordapp, WordDoc, PageRange: Variant;
i, nPageCounts, nStart, nEnd : Integer;
sContext: string;
begin
wordapp := CreateOleObject('Word.Application');
try
wordapp.Visible := False;
if OpenDialog1.Execute = False then Exit;

WordDoc := wordapp.Documents.Open(OpenDialog1.FileName);

//文档总页数
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 := -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);
wordapp.selection.copy;

if OleContainer1.canpaste then
OleContainer1.paste;

OleContainer1.Update;

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;
ShowMessage(sContext);
// wordapp.Selection.WholeStory;
// wordapp.selection.copy;

{ if olecontainer1.canpaste then
olecontainer1.paste;

OleContainer1.Update; }

//Sleep(3000);

nStart := -1;
nEnd := -1;
end;
finally
wordapp.Quit;
end;


[/code]
yct0605 2011-12-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 erhan 的回复:]
引用 3 楼 yct0605 的回复:
这个是打开word来控制的,我想要集成在form里面来实现啊!


哦,网上查了一下,要集成的话可以用OleContainer;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form……
[/Quote]
多谢帮忙啊,我在网上也找了一个,不过浏览的时候总是显示第一页和最后一页,能不能帮我看看那里有问题啊?

var
wordapp, WordDoc, PageRange: Variant;
i, nPageCounts, nStart, nEnd : Integer;
sContext: string;
begin
wordapp := CreateOleObject('Word.Application');
try
wordapp.Visible := False;
if OpenDialog1.Execute = False then Exit;

WordDoc := wordapp.Documents.Open(OpenDialog1.FileName);

//文档总页数
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 := -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);
wordapp.selection.copy;

if OleContainer1.canpaste then
OleContainer1.paste;

OleContainer1.Update;

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;
ShowMessage(sContext);
// wordapp.Selection.WholeStory;
// wordapp.selection.copy;

{ if olecontainer1.canpaste then
olecontainer1.paste;

OleContainer1.Update; }

//Sleep(3000);

nStart := -1;
nEnd := -1;
end;
finally
wordapp.Quit;
end;

erhan 2011-12-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yct0605 的回复:]
这个是打开word来控制的,我想要集成在form里面来实现啊!
[/Quote]

哦,网上查了一下,要集成的话可以用OleContainer;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtnrs, ExtCtrls, OleServer, Word2000;

type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
OleContainer1: TOleContainer;
WordDocument1: TWordDocument;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
aa,bb,cc,dd: OleVariant;
begin
OleContainer1.CreateObjectFromFile('d:\test.doc',false);
OleContainer1.Show;
OleContainer1.AutoActivate:=aaGetFocus;
OleContainer1.SetFocus;
WordDocument1.ConnectTo(IUnknown(OleContainer1.OleObject) as _Document);
aa := wdGoToPage;
bb := wdGoToAbsolute;
cc := '4';
dd := '';
WordDocument1.Application.Selection.GoTo_(aa, bb, cc, dd);
end;

end.

//另:如果在单一窗体里调用OLE很容易发生OLE把整个窗体都占据的情况。建立专用的OLE窗体来避免这样的情况放生,使OLE显示在第一个窗体指定的位置(panel)
//网上有相关的美化代码,可以查到的,如需,可以提供查到的连接。
yct0605 2011-12-27
  • 打赏
  • 举报
回复
这个是打开word来控制的,我想要集成在form里面来实现啊!

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi GAME,图形处理/多媒体
社区管理员
  • GAME,图形处理/多媒体社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧