使用前,先根据需要建立一个空的WORD文件作为模板,在模板文件中设置好各种格式和文本。另外,其中的PrnWordTable的参数是TDBGridEh类型的控件,取自Ehlib2.6示范代码如下:代码1:
替换打印模板中的“#TITLE#”文本为“示范代码1”
并且将DBGridEh1控件当前显示的内容插入到文档的末尾
在文档末尾插入一个空行
在文档末尾插入新的一行文本
将文档中的空行去掉  if PrnWordBegin('C:\打印模板.DOC','C:\目标文件1.DOC') then
  begin
    PrnWordReplace('#TITLE#','示范代码1');
    PrnWordTable(DBGridEh1);
    PrnWordInsert('');
    PrnWordInsert('这是新的一行文本');
    PrnWordReplace('^p^p','^p',true);
    PrnWordSave;
  end;

解决方案 »

  1.   

    忘了补充一点:PrnWordBegin里的shFileCopy也是自己编的函数,用于复制文件的。干脆也把代码贴出来吧:功能:安全的复制文件
    srcFile,destFile:源文件和目标文件
    bDelDest:如果目标文件已经存在,是否覆盖
    返回值:true成功,false失败
    }
    function shFileCopy(srcFile,destFile:String;bDelDest:boolean=true):boolean;
    begin
      result:=false;
      if not FileExists(srcFile) then
      begin
        ShowMessage('源文件不存在,不能复制。'+#10#13+srcFile);
        exit;
      end;
      if srcFile=destFile then
      begin
        ShowMessage('源文件和目标文件相同,不能复制。');
        exit;
      end;
      if FileExists(destFile) then
      begin
        if not bDelDest then
        begin
          ShowMessage('目标文件已经存在,不能复制。'+#10#13+destFile);
          exit;
        end;
        FileSetAttr(destFile,FileGetAttr(destFile) and not $00000001);
        if not DeleteFile(PChar(destFile)) then
        begin
          ShowMessage('目标文件已经存在,并且不能被删除,复制失败。'+#10#13+destFile);
          exit;
        end;
      end;
      if not CopyFileTo(srcFile,destFile) then
      begin
        ShowMessage('发生未知的错误,复制文件失败。');
        exit;
      end;
      //目标文件去掉只读属性
      FileSetAttr(destFile,FileGetAttr(destFile) and not $00000001);
      result:=true;
    end;
      

  2.   

    整理了一下,已经放到文档中心去了,标题为:[Delphi+Word]解决方案参考
    本来有些代码的处理方式还可以更灵活些,但实在没有时间再改进了,大家将就将就,呵呵