为什么delphi6的帮助里找不到关于TWordApplication的内容?那你能得到这些帮助?

解决方案 »

  1.   

    如何使用TWordApplication:
    最近接触了一个用户的案例,用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类似的产品了。
      

  2.   

    在DEIPHI中如何调用WORD打开WORD文档unit Uword;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, OleServer, Word97,ComObj, Buttons;type
      TForm1 = class(TForm)
        Button1: TButton;
        WordApplication: TWordApplication;
        WordDocument: TWordDocument;
        BitBtn1: TBitBtn;
        procedure Button1Click(Sender: TObject);
        procedure BitBtn1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
        Template,NewTemplate,ItemIndex:OleVariant;
        start,stop : olevariant ;
        myRange:range;
    Begin
        Template := EmptyParam;
        NewTemplate :=  True;
        ItemIndex := 1;
        start := 3 ;
        stop := 3 ;
        try
          Wordapplication.Connect;
        except
          MessageDlg('Word may not be installed', mtError, [mbOk], 0);
          Abort;
        end;
        Wordapplication.Visible := True;
        WordApplication.Caption := 'Delphi automation';
        {Create new document}
        Template := EmptyParam;
        NewTemplate := False;
          WordApplication.Documents.Add(Template, NewTemplate);
          WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
          WordDocument.Range.InsertAfter('报表'+#13);
          WordDocument.Range.InsertAfter(#13);
        //-- 指定插入表的起始处 -------- //
          myRange := WordDocument.Range(start,stop) ;
          WordDocument.Tables.Add(myRange,11,2);
          wordDocument.Tables.Item(1).Columns.Item(1).Width :=20; //改变表格列宽
          WordDocument.Range.InsertAfter('正文一:');
    end;procedure TForm1.BitBtn1Click(Sender: TObject);
    var 
        Word2000: Variant; 
    begin 
        try 
          Word2000:=CreateOleObject('word.basic'); 
          Word2000.FileNew; 
          Word2000.font('宋体'); //设置字体 
          Word2000.FontSize(14); //设置字号 
          Word2000.CenterPara; //居中        
          Word2000.Insert( '居中'+#13); 
          Word2000.LeftPara; //左对齐 
          Word2000.Insert( '左对齐'+#13); 
          Word2000.RightPara; //右对齐 
          Word2000.Insert( '右对齐'+#13); 
          Word2000.AppShow; //显示应用程序 
        except 
            showmessage('运行 Microsoft Word 失败!'); 
        end
    end;end.我只的文档是已经存在的。不是自己写一个新的。
    放两个组件,TWordApplication,TWordDocument.procedure TForm1.Button1Click(Sender: TObject);var    Template,NewTemplate,ItemIndex:OleVariant;Begin    Template := EmptyParam;    NewTemplate :=  false;    ItemIndex:=1;    WordApplication1.connect;    WordApplication1.visible:=true;//打开WORD    WordApplication1.Documents.Add(Template, NewTemplate);    WordDocument1.ConnectTo(WordApplication.Documents.Item(ItemIndex));        //打开文档end;