书签有两种一种是字符替换,一种是书签的直接替换,我把两种的源代码都给你。。
这个是计量所的报告处理的一部分,开始使用字符替换,后来使用书签替换,原理很简单,核心部分如下。
后面提供了字符替换函数。
注:word2000就是wordApplication.
function TFrmCertificate_Sample.ExCloneWord2000Simple(
FileName: String): Boolean;
var
FWordFile:OleVariant;
Password:OleVariant;
FInstrument:Table;
FCommandBarName,FCommandBar:OleVariant;
//-----------------
OleWhat,OleWhich,OleCount,OleName:OleVariant;
oleRange,OleComfirmCanvesions,OleLink,OleAttachment:OleVariant;
begin
Screen.Cursor:=crHourGlass;
if not DM.SaveToDocTemplet(DM.QWord,FileName) then Exit;
//开启WORD2000
try
Word2000.Connect;
except
ErrMessage('RPC远程接口出错!强烈建议去除杀毒软件的嵌入模式。');
Word2000.Disconnect;
Abort;
end;
//------------- 打开WORD2000------------------
try
FWordFile:=MyFrame.WordFile;
Word2000.Caption:='客户单位:'+MyFrame.ClientName+';'+
'器具名称:'+ed_Apparatus.Text+';'+
'出厂编号:'+ed_OutNo.Text+';'+
'规格型号:'+ed_Mode.Text+';'+
'制造厂商:'+ed_Manufactory.Text;
Word2000.Documents.OpenOld(FWordFile,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
//---------------替换字符---------------
省略。。。
[red] //-------------------添加模板---查找书签并且替换------------
OleWhat:=wdGoToBookmark; OleName:='Content';
OleWhich:=emptyenum; OleCount:=emptyenum;
Word2000.Selection.GoTo_(OleWhat,OleWhich,OleCount,OleName);
oleRange:=''; OleComfirmCanvesions:=False;
OleLink:=False; OleAttachment:=False;
Word2000.Selection.InsertFile(MyFrame.TempFile,oleRange,OleComfirmCanvesions,
OleLink,OleAttachment);[/red] //------显示WORD2000窗口--------------
Screen.Cursor:=crDefault;
Word2000.Visible:=True;
Word2000.ShowMe;
Word2000.WindowState:=wdWindowStateMaximize;
except
on E:Exception do
begin
ShowMessage('出错了,可能需要关闭所有的非法程序...'+#13+'错误号码:'+E.Message);
Word2000.Disconnect;
Exit;
end;//on:E
end;
end;
procedure TFrmCertificate_Sample.Word2000Instrument(var FTable: Table);
var
OleWhat,OleWhich,OleCount,OleName:OleVariant;
FRow,I:Integer;
FLevel:Integer;
begin
[brown] OleWhat:=wdGoToBookmark; OleName:='Instrument';//书签。
OleWhich:=emptyenum; OleCount:=emptyenum;
Word2000.Selection.GoTo_(OleWhat,OleWhich,OleCount,OleName);
FLevel:=Word2000.Selection.Tables.Item(1).NestingLevel;[/brown] if FLevel>1 then
Word2000.Selection.Tables.Item(1).Delete;
插入表格。。。
//----------------
end;
字符替换:
[blue]//procedure Word2000ReplaceText(FSection:Boolean;OldText,ReplaceText:String); //[/blue]保留函数
{
procedure TFrmCertificate_do.Word2000ReplaceText(FSection:Boolean;OldText,
ReplaceText: String);
//fSection表示替换的是否是页眉。。默认为FALSE。
var
OleForword,OleRelaceOption:OleVariant;
OleFindText,OleReplaceText:OleVariant;
begin
OleForword:=wdForward;
OleRelaceOption:=wdReplaceAll;
OleFindText:=OldText;
OleReplaceText:=ReplaceText;
if FSection then
Word2000.Selection.Sections.Item(1).Headers.Item(1).Range.Find.ExecuteOld(
OleFindText,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
OleForword, EmptyParam,EmptyParam,OleReplaceText,OleRelaceOption)
else
Word2000.Selection.Range.Find.ExecuteOld(OleFindText,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,OleForword, EmptyParam,EmptyParam,
OleReplaceText,OleRelaceOption);
end;
}