我想在程序中实现对Word已有模板的调用,或者就在程序中自动生成Word模板,然后调用之。是不是得利用Server组件面板上的相关控件,如果是,能否提供相关控件具体的使用方法?如果不是,我想就的利用系统程序调用了吧,应该在系统中安装有word,然后调用之,打开相应的文档或生成相应的文档。这在Delphi 6里该如何实现??请各位老师,大侠指教!

解决方案 »

  1.   

    利用现有的组件编程,这样来得快一点,你可以参看delphi的书啊,关于服务组件的使用。也可以看帮助。
      

  2.   

    以前用过的一个函数,也许对你有帮助。uses ComObj;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      FindText, WordObject, MatchCase, MatchWholeWord, MatchWildcards,
      MatchSoundsLike, MatchAllWordForms, Forward1, Wrap, Format,
      Replacewith, Replaceall: OleVariant;
    begin
      FindText := '#AAA#';//被替换字符
      MatchCase := False;
      MatchWholeWord := True;
      MatchWildcards := False;
      MatchSoundsLike := False;
      MatchAllWordForms := False;
      Forward1 := True;
      Wrap := wdFindContinue;//=1
      Format := False;
      ReplaceWith := '举交字第001号';//替换字符
      Replaceall := True;
      WordObject:=CreateOLEObject('Word.Application');
      WordObject.Visible := True;
      try
        WordObject.Documents.open('C:\AAA.doc',False);
        WordObject.Application.ActiveDocument.Range.Find.Execute(
          FindText, MatchCase, MatchWholeWord,
          MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward1,
          Wrap, Format, ReplaceWith, Replaceall
        );
      finally
        WordObject.Application.ActiveDocument.PrintOut;
      end;
    end;
      

  3.   

    olecontainner!!看看这方面的东东!!不难!!
      

  4.   

    读取和显示Word文档的VCL控件:
    http://www.ksdev.com/vcl/index.html
    http://vcl.vclxx.org/DELPHI/D32FREE/DATA2WRD.ZIP最近接触了一个用户的案例,用delphi控制word做一个合同管理程序。办公人员先根据业务需要,写好合同的文字,但在用户名称、产品名称等变化的位置填写指定的标记字符串,然后通过delphi把数据库中的实际数据替换掉word中的文字,最后让word打印出合同。 delphi自带了一个简单的word例题,但功能太简单。通过查找vba的说明,再对照delphi的vcl,编写了如下代码,实现了基本的公文管理功能。 启动word时用如下代码: 
    begin
    try 
    wordapplication.connect; 
    except 
    messagedlg('word may not be installed', mterror, [mbok], 0); 
    abort; 
    end; 
    wordapplication.visible := true; 
    wordapplication.caption := 'delphi automation'; 
    end; 关闭word用如下代码。如果想保存doc文件,请修改savechanges变量的内容: 
    var 
    savechanges, originalformat, routedocument: olevariant; 
    begin 
    savechanges := wddonotsavechanges; 
    originalformat := unassigned; 
    routedocument := unassigned; 
    try 
    wordapplication.quit(savechanges, originalformat, routedocument); 
    wordapplication.disconnect; 
    except 
    on e: exception do 
    begin 
    showmessage(e.message); 
    wordapplication.disconnect; 
    end; 
    end; 
    end; 让word打开一个指定的文件,需要先放置opendialog,然后调用wordapplication.documents.open: 
    var 
    itemindex :olevariant; 
    filename, confirmconversions, readonly, addtorecentfiles, 
    passworddocument, passwordtemplate, revert, 
    writepassworddocument, writepasswordtemplate, format: olevariant; 
    begin 
    if not dlgopen.execute then 
    exit; {open document} 
    filename := dlgopen.filename; 
    confirmconversions := false; 
    readonly := false; 
    addtorecentfiles := false; 
    passworddocument := ''; 
    passwordtemplate := ''; 
    revert := true; 
    writepassworddocument := ''; 
    writepasswordtemplate := ''; 
    format := wdopenformatdocument; wordapplication.documents.open( filename, confirmconversions, 
    readonly, addtorecentfiles, passworddocument, passwordtemplate, 
    revert, writepassworddocument, writepasswordtemplate, format ); {assign worddocument component} 
    itemindex := 1; 
    worddocument.connectto(wordapplication.documents.item(itemindex)); {turn spell checking of because it takes a long time if enabled and slows down winword} 
    wordapplication.options.checkspellingasyoutype := false; 
    wordapplication.options.checkgrammarasyoutype := false; 
    end; 让word替换标记字符串要使用worddocument.range.find.execute,这里用delphi替换了<#name>: 
    var 
    findtext, matchcase, matchwholeword, matchwildcards, matchsoundslike, 
    matchallwordforms, forward, wrap, format, replacewith, replace: olevariant; 
    begin 
    findtext := '<#name>'; 
    matchcase := false; 
    matchwholeword := true; 
    matchwildcards := false; 
    matchsoundslike := false; 
    matchallwordforms := false; 
    forward := true; 
    wrap := wdfindcontinue; 
    format := false; 
    replacewith := 'delphi'; 
    replace := true; worddocument.range.find.execute( findtext, matchcase, matchwholeword, 
    matchwildcards, matchsoundslike, matchallwordforms, forward, 
    wrap, format, replacewith, replace ); end; 上面这4段代码完成了公文管理的基本功能,再把它和数据库结合起来,就可以开发一个与lotus notes类似的产品了。