在Excel和PowerPoint文档中使用OLEsearch和replace

我成功地编写了这个代码在MSWord文档中执行search和replace。 现在我需要为xls,xlsx,ppt和pptx做这个工作。

这个想法是“parsing所有的文件,并find每个stringreplace它”。

想象一下,我想用实际的用户名replacestring“ <MY_USER_NAME>" ,这个string是在Excel工作表1,2或3还是在特定的Powerpoint页面中并不重要。

我google代码,但我只是发现小实验,有没有人有更多的经验呢?

谢谢。

我已经在我的项目中编写了以下过程,用Delphi程序中的Excel中的值replace标签。 它取代了所有表单上的所有标签

OutF – 是Excel Ole对象,Slabel – 是要replace的标记,SValue – 是replace标记的值。

例如

 OutF := CreateOleObject('Excel.Application' ); ...... ExcelOutStr(OutF,'<MY_USER_NAME>','Value for MY User Name'); 

这里是程序:

 procedure ExcelOutStr(OutF:Variant;SLabel,SValue:String); var i,j:integer; begin try OutF.DisplayAlerts := false; //To place a string with linebreaks into one Cell SValue:=StringReplace(SValue,#13#10,#10,[rfReplaceAll, rfIgnoreCase]); for j:=1 to OutF.Sheets.Count do begin OutF.WorkSheets[j].Select; if length(SValue)<250 then begin OutF.Cells.Replace(What:=Slabel, Replacement:=SValue, LookAt:=2,SearchOrder:=1, MatchCase:=False); end else begin //Excel .replace fails on string with length >250 so replace it in few steps i:=1; while i<=length(SValue) do begin if i+200-1<length(SValue) then OutF.Cells.Replace(What:=Slabel, Replacement:=Copy(SValue,i,200)+SLabel, LookAt:=2,SearchOrder:=1, MatchCase:=False) else OutF.Cells.Replace(What:=Slabel, Replacement:=Copy(SValue,i,200), LookAt:=2,SearchOrder:=1, MatchCase:=False); i:=i+200; end; end; end; OutF.WorkSheets[1].Select; except on E : Exception do ShowMessage('Error: Lablel ['+SLabel+'] '+E.Message); end; end;