有一个word文档带图片和文字信息(页数不超过10页),要用delphi显示word的内容(默认显示的是第一页内容),并控制word翻页显示,多谢大家了!
解决方案 »
- 关于debug api.寄存器的问题.分数可以再加.在线紧急等待..
- 请问哪里有ApdComPort的控件下载?急!
- 用ActiveX可以实现把Word的强大的文字功能集成到IE中吗? 若有实例,愿给1000分赠送!来者讨论也有分!!
- 这问题真的没有人能解决吗????
- 急聘delphi+sqlServer , 实际工作经验一年以上。学历无要求 。年龄无要求。
- sql批量更新的代码 提示没有默认值
- 强烈建议建立一个dxdbgrid的应用专题(不知有没有,我没搜过),因为它太强大了
- 关于文本文件的读取问题.
- 高手 帮忙 (小问题)
- 做的邮件客户端现在不能收发GMAIL的邮件了
- 请大家看看这两个函数是否存在资源释放方面的问题?
- IDHTTP关于COOKIE的几个问题
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你懂的。
哦,网上查了一下,要集成的话可以用OleContainer;unit Unit1;interfaceuses
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)
//网上有相关的美化代码,可以查到的,如需,可以提供查到的连接。
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=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]
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);取消注释即可。
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;