我想使用delphi打开word,并且将richedit控件生成文本加入到里面。请高手指点啊。谢谢。

解决方案 »

  1.   

    打开word的方法:
    procedure TFormd.Label1Click(Sender: TObject);
    var
    exename,pname:string;
    begin
     exename:=ExtractFilePath(ExpandFileName('mydata.exe'));
     pname:=exename+'\documents\';
    filenametmp:=label1.Caption
    filenamedoc:=(pname)+filenametmp+'.doc';begin
          if FileExists(filenamedoc) then
             begin
             ShellExecute(0, nil,Pchar(filenamedoc),
             nil, nil, SW_NORMAL);
             end
          else                           
             
    begin
        if MessageDlg('当前文件不存在,你想建立该文件吗?',
        mtConfirmation, [mbYes, mbNo], 0) = mrYes then
             begin
                filename:=(pname)+(filenametmp)+'.doc';
                      filehandle:=FileCreate(filename);
                      fileclose(filehandle);
                      ShellExecute(0, nil,Pchar(filename),
                      nil, nil, SW_NORMAL);
                    end
                else                  Application.MessageBox('已经终止', '讯息', 48)
              end    endend;
      

  2.   

    1.添加server页空间WordApplication
    procedure TForm1.Button2Click(Sender: TObject);
    var
      Template, NewTemplate,DocType,DocVisible : OleVariant;
      ItemIndex,Format: OleVariant;
    begin
      richedit.SelectAll;
      richedit.CopyToClipboard;  Template := EmptyParam;
      NewTemplate := EmptyParam;
      DocType := EmptyParam;
      DocVisible := False;  WordApp.Connect;
      WordApp.Visible := False;
      WordApp.Documents.Add(Template,NewTemplate,DocType,DocVisible);
      ItemIndex := 1;
      WordDoc.ConnectTo(WordApp.Documents.Item(ItemIndex));
      WordDoc.Range.Paste;
      Format := wdFormatDocument;
      WordDoc.Disconnect;
    end;