Delphi.docx
- 文档编号:4889954
- 上传时间:2022-12-11
- 格式:DOCX
- 页数:19
- 大小:17.83KB
Delphi.docx
《Delphi.docx》由会员分享,可在线阅读,更多相关《Delphi.docx(19页珍藏版)》请在冰豆网上搜索。
Delphi
delphi操作word基类,插入表格,分页符,日期,页码,替换,图片
unitutmyword;
(*
by闫磊编写2004.09.06email:
landgis@yanleigis@
*)
interface
usesword2000,activex,variants,dialogs,sysutils;
type
tmyword=class(tobject)
private
fword:
twordapplication; //word对象
fdoc:
tworddocument; //文档对象
procedureconnectword();
public
//换行
procedureaddreturn();
//设置字体
proceduresetfont(fontname:
string;fontsize:
integer;
fontbold:
boolean=false;fontunderline:
boolean=false);
//插入表格
functionaddtable(col,row:
integer):
table;
//设置背景色
proceduresettableback(backcolor:
toleenum);
//鼠标下移
proceduremovedown(num:
integer=1);
//选择下移
procedureselectdown(num:
integer=1);
//鼠标上移
proceduremoveup();
//鼠标右移
proceduremoveright(num:
integer=1);
//选择右移
procedureselectright(num:
integer=1);
//写字
procedurewritetext(text:
string);
//按样式插入一行记录
//如addline"备注","正文"
//addline'kk',"标题1"
procedureaddline(s:
string;pstyle:
olevariant);
procedurealignline(align:
toleenum);
//插入分页符
procedureinsertpage();
//插入插入页码
procedureinsertpagenum();
//插入日期
procedureinsertdate();
//设置表格一列宽度
proceduresettable(ptable:
table;columnidx:
integer;width:
integer);
//设置表格一列高度
proceduresettableheight(ptable:
table;rowidx:
integer;height:
integer);
//插入目录
procedureinsertcontents();
//创立书签
procedurecreatebookmark(bookmarkname:
string);
//移动到标签
proceduregotobookmark(bookmarkname:
string);
//粘贴
procedurepaste();
//替换
procedurereplace(source,target:
string);
//保存
proceduresave(filename:
string);
//插入图片
procedureaddpicture(filename:
string);
//移到最后
proceduremoveend();
//合并
procedurecellsmerge();
constructorcreate;
destructordestroy;override;
end;
//functionmyword:
tmyword;
implementation
//var
// fmyword:
tmyword;
{functionmyword:
tmyword;
begin
iffmyword=nilthen
begin
fmyword:
=tmyword.create;
end;
result:
=fmyword;
end;
}
constructortmyword.create();
begin
inheritedcreate;
connectword();
//
end;
proceduretmyword.connectword();
var
b:
boolean;
begin
b:
=false;
if(fword=nil)then
b:
=true
else
begin
try
fdoc.activate;
except
b:
=true;
end;
end;
ifnotbthenexit;
fword:
=twordapplication.create(nil);//word对象
fdoc:
=tworddocument.create(nil); //文档对象
fword.connect;
fdoc.activate;
fword.visible:
=true;
end;
destructortmyword.destroy;
begin
//
//fdoc.saveas('c:
\1.doc');
fdoc.free;
fword.disconnect;
//退出一定退出word,byyl2005.2.2
//fword.quit;
//fword.free;
inheriteddestroy;
end;
proceduretmyword.addreturn();
begin
try
fword.selection.typeparagraph;
except
addreturn();
end;
end;
proceduretmyword.setfont(fontname:
string;fontsize:
integer;fontbold:
boolean
=false;fontunderline:
boolean=false);
begin
try
//connectword();
fword.selection.font.name:
=fontname;
fword.selection.font.size:
=fontsize;
iffontboldthen
fword.selection.font.bold:
=wdtoggle;
iffontunderlinethen
fword.selection.font.underline:
=wdunderlinesingle;
except
setfont(fontname,fontsize,fontbold,fontunderline);
end;
end;
//插入表格
functiontmyword.addtable(col,row:
integer):
table;
var
defaulttable:
olevariant;
begin
try
//connectword();
defaulttable:
=1;
result:
=fdoc.tables.add(fword.selection.range,row,col,defaulttable,
emptyparam);
except
showmessage(format('列%d,行%d',[col,row]));
addtable(col,row);
end;
end;
//设置背景色
proceduretmyword.settableback(backcolor:
toleenum);
begin
try
//connectword();
fword.selection.cells.shading.backgroundpatterncolor:
=backcolor;
except
settableback(backcolor);
end;
end;
//选择下移
proceduretmyword.selectdown(num:
integer=1);
var
unit_:
olevariant;
count:
olevariant;
extend:
olevariant;
begin
try
//connectword();
count:
=num;
unit_:
=wdline;
extend:
=wdextend;
fword.selection.movedown(unit_,count,extend);
except
movedown();
end;
end;
//鼠标下移
proceduretmyword.movedown(num:
integer=1);
var
unit_:
olevariant;
count:
olevariant;
extend:
olevariant;
begin
try
//connectword();
count:
=num;
unit_:
=wdline;
fword.selection.movedown(unit_,count,extend);
except
movedown();
end;
end;
//鼠标上移
proceduretmyword.moveup();
var
unit_:
olevariant;
count:
olevariant;
extend:
olevariant;
begin
unit_:
=wdline;
count:
=1;
fword.selection.moveup(unit_,count,extend);
end;
//选择右移
proceduretmyword.selectright(num:
integer=1);
var
unit_:
olevariant;
count:
olevariant;
extend:
olevariant;
begin
try
//connectword();
unit_:
=wdcharacter;
count:
=num;
extend:
=wdextend;
fword.selection.moveright(unit_,count,extend);
except
moveright();
end;
end;
//鼠标右移
proceduretmyword.moveright(num:
integer=1);
var
unit_:
olevariant;
count:
olevariant;
extend:
olevariant;
begin
try
//connectword();
unit_:
=wdcell;
count:
=num;
fword.selection.moveright(unit_,count,extend);
except
moveright();
end;
end;
//写字
proceduretmyword.writetext(text:
string);
begin
try
//connectword();
fword.selection.typetext(text);
except //防止呼叫失败
writetext(text);
end;
end;
//按样式插入一行记录
//如addline"备注","正文"
//addline'kk',"标题1"
proceduretmyword.addline(s:
string;pstyle:
olevariant);
proceduresetstyle(pstyle:
olevariant);
var
outstyle:
style;
v:
olevariant;
begin
outstyle:
=fword.activedocument.styles.item(pstyle);
v:
=outstyle;
fword.selection.set_style(v);
end;
begin
writetext(s); //加入一行
try
setstyle(pstyle);
except
setstyle(pstyle);
end;
end;
proceduretmyword.alignline(align:
toleenum);
begin
fword.selection.paragraphformat.alignment:
=align;
end;
//插入分页符
proceduretmyword.insertpage();
var
_type:
olevariant;
begin
_type:
=7;
fword.selection.insertbreak(_type);
end;
//插入日期
proceduretmyword.insertdate();
var
datetimeformat:
olevariant;
insertasfield:
olevariant;
insertasfullwidth:
olevariant;
datelanguage:
olevariant;
calendartype:
olevariant;
begin
try
insertasfield:
=false;
insertasfullwidth:
=false;
datetimeformat:
='yyyy''年''m''月''d''日''';
datelanguage:
=wdsimplifiedchinese;
calendartype:
=wdcalendarwestern;
fword.selection.insertdatetime(datetimeformat,insertasfield,
insertasfullwidth,datelanguage,calendartype);
except
insertdate();
end;
end;
//插入页码
proceduretmyword.insertpagenum();
var
psection:
section;
pagenumberalignment:
olevariant;
firstpage:
olevariant;
begin
psection:
=fword.selection.sections.item
(1);
pagenumberalignment:
=1; //中间
firstpage:
=true;
psection.footers.item(wdheaderfooterprimary).pagenumbers.add(pagenumberalignment,firstpage);
end;
//设置表格一列高度
proceduretmyword.settableheight(ptable:
table;rowidx:
integer;height:
integer);
var
prow:
row;
begin
prow:
=ptable.rows.item(rowidx);
prow.setheight(height,wdadjustnone);
end;
//设置表格一列宽度
proceduretmyword.settable(ptable:
table;columnidx:
integer;width:
integer);
begin
ptable.columns.item(columnidx).setwidth(width,wdadjustnone);
end;
//插入目录
proceduretmyword.insertcontents();
var
prange:
range;
useheadingstyles:
olevariant;
upperheadinglevel:
olevariant;
lowerheadinglevel:
olevariant;
usefields:
olevariant;
tableid:
olevariant;
rightalignpagenumbers:
olevariant;
includepagenumbers:
olevariant;
addedstyles:
olevariant;usehyperlinks:
olevariant;
hidepagenumbersinweb:
olevariant;
begin
withfword.activedocumentdo
begin
prange:
=fword.selection.range;
rightalignpagenumbers:
=true;
useheadingstyles:
=true;
upperheadinglevel:
=1;
lowerheadinglevel:
=3;
includepagenumbers:
=true;
usehyperlinks:
=true;
hidepagenumbersinweb:
=true;
tablesofcontents.add(prange,useheadingstyles,
upperheadinglevel,
lowerheadinglevel,
usefields,
tableid,
rightalignpagenumbers,
includepagenumbers,
addedstyles,usehyperlinks,
hidepagenumbersinweb);
tablesofcontents.item
(1).tableader:
=wdtableaderdots;
tablesofcontents.format:
=wdindexindent;
end;
end;
//创立书签
proceduretmyword.createbookmark(bookmarkname:
string);
var
prange:
olevariant;
begin
prange:
=fword.selection.range;
withfword.activedocument.bookmarksdo
begin
add(bookmarkname,prange);
defaultsorting:
=wdsortbyname;
showhidden:
=false;
end;
end;
//移动到标签
proceduretmyword.gotobookmark(bookmarkname:
string);
var
what:
olevariant;
which:
olevariant;
count:
olevariant;
name:
olevariant;
begin
what:
=wdgotobookmark;
name:
='begin';
fword.selection.goto_(what,
which,
count,
name)
end;
//粘贴
proceduretmyword.paste();
begin
fword.selection.paste;
end;
//替换
proceduretmyword.replace(source,target:
string);
var
findtext:
olevariant;
pwrap:
olevariant;
replacewith:
olevariant;
replace:
olevariant;
begin
fword.selection.find.clearformatting;
fword.selection.find.replacement.clearformatting;
findtext:
=source;
pwrap:
=wdfindcontinue;
replacewith:
=target;
replace:
=wdreplaceall;
withfword.selection.finddo
begin
{text:
=source;
replacement.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Delphi