查找word中的某一关键字,然后在这个关键字的后面插入新的内容。
我的问题是什么函数可以定位这个关键字的位置

解决方案 »

  1.   

    好东西大家分享。给你一个单元,可以控制向word的模板文件的任意位置写入指定的任意数据  
    注意,模板需要写入数据的地方需要用$$$_1之类的标志标明。 
    unit  unAppWord;  
     
    interface  
    uses  
       Windows,  Messages,  SysUtils,  Variants,  Classes,  Graphics,  Controls,  Forms,  
       Dialogs,  udbword,  ADODB,  DB;  
       //ComObj,  Word2000,  
    type  
       TAppword  =  class(TdbWord)  
       private  
           dbWord:  TdbWord;  
    //      Protected  
       public  
           constructor  Create();  
           destructor  Destroy();  override;  
           //依据Word文件名和内容向文件写内容  
           procedure  WriteDoc(sts:  Tstrings;  fn:  string);  
           //提取模板文件并生成word文件  
           function  PickDoc(DocTitle:  string;  Conn:  TADOConnection):  string;  
             {  Public  declarations  }  
       end;  
     
     
    implementation  
    const  
       ReplaceText  =  '$$$_';  
    constructor  TAppWord.Create();  
     
    begin  
       inherited  Create();  
    end;  
     
    destructor  TAppWord.Destroy();  
    begin  
       inherited  Destroy();  
    end;  
     
    procedure  TAppWord.WriteDoc(sts:  Tstrings;  fn:  string);  
    var  
       sCnt:  string;  
       i:  integer;  
    begin  
       if  (fn  =  '')  or  (not  Assigned(sts))  then  exit;  
       try  
       dbword.OpenFile(fn);  
           for    i  :=  0  to  sts.Count  -  1  do  
           begin  
               if  sts.Strings[i]  =  ''  then  
               begin  
                   sCnt  :=  '  ';  
                   dbword.Replace(ReplaceText+IntToStr(i),sCnt);  
               end  
               else  
               begin  
                   sCnt  :=  sts.Strings[i];  
                   dbword.Replace(ReplaceText+IntToStr(i),sCnt);  
               end;  
           end;  
           dbword.Save;  
           dbword.CloseDoc;  
       except  
             raise(Exception.Create('error'));  
       end;  
     
    end;  
     
    function  TAppWord.PickDoc(DocTitle:  string;  Conn:  TADOConnection):  string;  
    var  
       AQuery:  TAdoQuery;  
       sqlDoc:  string;  
       SD:  TSaveDialog;  
     
    begin  
       result  :=  '';  
       if  (Conn  =  nil)  or  (DocTitle  =  '')  or  (not  conn.Connected)  then  exit;  
     try  
           AQuery  :=  TAdoQuery.Create(nil);  
           AQuery.Connection  :=  Conn;  
           if  AQuery.Active  then  AQuery.Close;  
           AQuery.SQL.Clear;  
           sqlDoc  :=  Format('select  MB_DATA  from  tdly.MB_WDWJ  where  MB_NAME=''%s''',  [DocTitle]);  
           AQuery.SQL.Add(sqlDoc);  
           AQuery.Open;  
           if  AQuery.RecordCount    <  >  1  then  
           begin  
               Alert('无法提取'+  DocTitle  +'模板文件');  
               exit;  
           end;  
           SD  :=  TSaveDialog.Create(nil);  
           try  
               SD.Filter  :=  'WORD文件(*.DOC)  |*.DOC';  
               sd.DefaultExt  :=  '*.doc';  
               sd.Options  :=  [ofPathMustExist,  ofOverwritePrompt];  
               if  not  SD.Execute  then  exit;  
               result  :=  Sd.FileName;  
               Tblobfield(AQuery.FieldByName('MB_DATA')).SaveToFile(Result);//  .SaveToStream(memstr);  //模板字段  
           finally  
               AQuery.Close;  
               AQuery.Free;  
               SD.Free;  
           end;  
       except  
       end;  
    end;  
     
     
    end. 
    procedure TFrmMain.BitBtn12Click(Sender: TObject);
    var
    //ItemIndex:OleVariant;
    FileName,ConfirmConversions,ReadOnly,AddToRecentFiles,
    PasswordDocument,PasswordTemplate, Revert,
    WritePasswordDocument,WritePasswordTemplate,Format:OleVariant;
    begin
                 WordApplication1.Connect;
                 FileName:='c:\ZhiD.doc';
                 ConfirmConversions:=False;
                 ReadOnly:=False;
                 AddToRecentFiles:=False;
                 PasswordDocument:='';
                 PasswordTemplate:='';
                 Revert:=True;
                 WritePasswordDocument:='';
                 WritePasswordTemplate:='';
                 Format:=wdOpenFormatDocument;
                 WordApplication1.Documents.Open(FileName,ConfirmConversions,ReadOnly,AddToRecentFiles,PasswordDocument,PasswordTemplate,Revert,WritePasswordDocument,WritePasswordTemplate,Format,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
                 WordApplication1.Visible:=true;
      

  2.   

    Selection.Find.ClearFormatting
    {selection是文档被选中的部分,find是“查找”之一功能实现的主要函数,ClearFormatting是清除上一次查找的设置。“查找”这一功能的实现有些像SQL,先设置,后执行}
        With Selection.Find {设置执行查找的参数}
            .Text = "direct"{要查找的文字,这里是一个例子,我们要查direct这个单词}
            .Replacement.Text = ""
                                  {查找和替换的功能是作在一起的,这里就是替换后的
                                   文字,因为我们不执行替换,所以这里是空字符串}
            .Forward = True{查找的方向,向前或向后}
            .Wrap = wdFindContinue{这里设置,搜索执行道文件头或尾时是否从头继续}
            .Format = False{如果该属性值为 True,则在查找操作中包含格式。}
            .MatchCase = False{如果为 True,则查找操作区分大小写。}
            .MatchWholeWord = False
                                   {如果本属性为True,则查找操作将只查找完整单词,
                                    而不是一个长单词的一部分。}
            .MatchByte = True
                              {如果该属性值为 True,则 Microsoft Word 在搜索过程中
                               将区分全角和半角的字符或字母。}
            .MatchWildcards = False
                                   {如果本属性为 True,则待查找文字中包含查找通配符。
                                    对应于“编辑”菜单“查找和替换”对话框中的“使用
                                    通配符”复选框。}
            .MatchSoundsLike = False
                                      {如果为 True,则查找操作返回与待查找文字发音
                                       相近的单词。}
            .MatchAllWordForms = False
                                      {如果为 True,则在查找操作需查找文本的所有形
                                      式(例如,如果要查找的单词是“sit”,那么也
                                       查找“sat”和“sitting”)。}
        End With
        Selection.Find.Execute{执行查找}
    这是VBA形式的一段有关在WORD文档中定位的程序,在DELPHI中需要改一下,主要把语法形式改了就行了,其他的没什么太大的区别
      

  3.   

    to vavyboyleon(一天到晚加班的驴) :
    Selection.Find.Execute的返回值是boolean类型,我怎样知道当前查找的内容在文档中的位置?